Posts

Showing posts with the label android-fragments

Display MyFragment to MainActivity

Display MyFragment to MainActivity I have two java classes. One is main activity and other is MyFragment . MyFragment contains a fragment which has a map. I want to display this to my MainActivity . MainActivity Consists of a Navigation Drawer activity. my code for both the classes are: main activity MyFragment MyFragment MainActivity MainActivity `public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncStat...

Trying to create a app that has 3 navigation buttons at the buttom and a new activity comes up when corresponding button is

Trying to create a app that has 3 navigation buttons at the buttom and a new activity comes up when corresponding button is I'm trying to create a app that navigates in the same way movie pass app does. At the button it has 3 buttons that stay there and a new Activty comes up when you press the buttons. I could put these buttons on all 3 activtes, but then if I make a code change to the buttons it must be changed in three places. 2 Answers 2 Two options could help with this. 1) Use a fragment for the content shown above the three buttons so you just have a single activity, and the buttons change which fragment is shown. 2) If you are using a BottomNavigationView or some other view that takes a listener you can create your own OnNavigationItemSelectedListener implmentation that handles all the logic for what happens on button presses. Then that code is in a single place, and you can use that l...

How to copy YouTube's app navigation logic

How to copy YouTube's app navigation logic I want to implement in my app navigation logic like in Youtube app. (BottomNavigationView + Fragment management). I want this, because these fragments are heavy, so I want them to be lazy initialized and then stored in backstack, I feel like YouTube is doing it this way. I have implemented BottomNagivationView but I have problems with Fragment Management. My code: bottomNavigationView.setOnTabSelectedListener { position, _ -> setFragment(OnlinePageFragment.Page.values()[position]) } where Pages is enum enum class Page(index: Int, val klass: Class<*>) { ONE(0, OnePageFragment::class.java), TWO(1, TwoPageFragment::class.java), THREE(2, ThreePageFragment::class.java) } and here is my setFragment function fun setFragment(page: OnlinePageFragment.Page) { var fragment: Fragment? = supportFragmentManager.findFragmentByTag(page.klass.name) val tag = page.klass.name if (fragment == null) ...