Start an activity from fragment using bottom navigation bar - android

In my app I have 5 fragments. In order to switch between them i use bottom navigation bar.
Also, i need to open activities from my fragments, but when I open new activity it opens in each of the fragments. (I mean over the fragments, in each case)
How to make the activity open in one case?
My code:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
fragmentManager.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.nav_bookmark:
fragmentManager.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.nav_blog:
fragmentManager.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
case R.id.nav_notification:
fragmentManager.beginTransaction().hide(active).show(fragment4).commit();
active = fragment4;
return true;
case R.id.nav_account:
fragmentManager.beginTransaction().hide(active).show(fragment5).commit();
active = fragment5;
return true;
}
return false;
}
};
Thanks!

This is clearly a FragmentManager issue - and not an Activity issue.
Instead .hide().show() use .replace().
For reference, see the documentation.

According to the Android documentation, an activity is always placed on top of the current activity stack.
So if you have the MainActivity with the BottomNavigationView and the fragments, starting a new Activity will open the activity above the MainActivity with the BottomNavigationView. So it is not possible to open an Activity only in one of the BottomNavigationView Fragments.
To achieve the desired behaviour, consider using another Fragment instead.

Related

Android Bottom Navigation Bar in activities

I am developing a car rental app and right now I have 3 fragments in my app (Home - where all the cars are displayed from a recyclerview , Orders - where user's bookings are shown , Profile - user profile). I can switch beetween these 3 fragments from my bottom navigationbar.
Now I created onclick listeners for every car on home page. When I click on a car it sends me to a new activity "activity_car_detail.xml" which contains specific information about the selected car. My question is: how can I make the navigation bar to appear on this Car Detail activity? Should I use fragment instead of activity or what should I do?
This is the code for the Main Activity which contains the bottom navigation bar:
public class MainActivity extends AppCompatActivity {
public static ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_orders:
selectedFragment = new OrdersFragment();
break;
case R.id.nav_profile:
selectedFragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}
I suggest using the Navigation component instead of Fragment transactions. Also this will fix your issue and it will remove boilerplate code. Read more about it's documentation here.

Layouts sometimes overlap in android fragment

My code works very well
But sometimes layouts overlap
The reason for this code is that the first fragment should not be reloaded.
And the second and third fragments should always be reloaded
Have you ever had problems with layout overlapping code?
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem)
{
Fragment selectedFragment = null;
switch (menuItem.getItemId())
{
case R.id.timer:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
break;
case R.id.favorite:
fm.beginTransaction().hide(active).commit();
fm.beginTransaction().detach(fragment2).attach(fragment2).commit();
fm.beginTransaction().show(fragment2).commit();
active = fragment2;
break;
case R.id.calendar:
fm.beginTransaction().hide(active).commit();
fm.beginTransaction().detach(fragment3).attach(fragment3).commit();
fm.beginTransaction().show(fragment3).commit();
active = fragment3;
break;
}
return true;
}
As per my understanding you have to show one screen based on menu click so why not you're using FrameLayout in xml and based on click or any event just add or replace the fragment. Make 3 different Fragment class and use interface or any latest arch component provide by Google.

Android Show Hide Fragment BottomNavigationView First Added Item Not Visible

I have a setup of 3 Fragments inside my main activity and want to navigate them using the BottomNavigationView. I want to add them once and then just switch between them without actually destroying the Fragments. Everything works fine except that the first fragment added to the SupportFragmentManager always disappears (tried changing the order so the problem is not with the Fragments themselves). Actually, it doesn't disappear but the last Fragment that occupied the container shows up.
Eg. I go to position 3, Fragment 3 shows up in the container and then click on position 1, Fragment 3 will still occupy the container. But if I tap position 2 Fragment 2 will appear. How I'm adding the Fragments:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, fragment1); //whatever gets added here ends up being invisible
ft.commitAllowingStateLoss();
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, fragment2);
ft.commitAllowingStateLoss();
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, fragment3);
ft.commitAllowingStateLoss();
How I'm switching between them using the OnNavigationItemSelectedListener:
private BottomNavigationView.OnNavigationItemSelectedListener navigation_listener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
Fragment previousFragment = getSupportFragmentManager().findFragmentById(R.id.container);
switch(item.getItemId()){
case R.id.bottom_navigation_bar_position1:
selectedFragment = fragment1;
break;
case R.id.bottom_navigation_bar_position2:
selectedFragment = fragment2;
break;
case R.id.bottom_navigation_bar_position3:
selectedFragment = fragment3;
break;
}
getSupportFragmentManager().beginTransaction().hide(previousFragment).commit();
getSupportFragmentManager().beginTransaction().show(selectedFragment).commit();
return true;
}
};
The XML menu navigation:
<item
android:id="#+id/bottom_navigation_bar_position1"
android:title="Title1"
android:icon="#drawable/ic1"
/>
<item
android:id="#+id/bottom_navigation_bar_position2"
android:title="Title2"
android:icon="#drawable/ic2"
/>
<item
android:id="#+id/bottom_navigation_bar_position3"
android:title="Title3"
android:icon="#drawable/ic3"
/>
Btw. just using replace() instead of show and hide works but that's not the goal...
I've also tested whether the first added Fragment exits (and doesn't get destroyed) and it indeed exists...
Thanks!
Instead of trying to show and hide fragment I solved the problem by managing the Fragments using a ViewPager and a SectionsPageAdapter. Once you link these two, use ViewPager.setOffscreenPageLimit(3) to make sure no Fragments get destroyed. My BottomNavigationView.OnNavigationItemSelectedListener looks like this now:
private BottomNavigationView.OnNavigationItemSelectedListener navigation_listener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.bottom_navigation_bar_position1:
tab_view_pager.setCurrentItem(0);
return true;
case R.id.bottom_navigation_bar_position2:
tab_view_pager.setCurrentItem(1);
return true;
case R.id.bottom_navigation_bar_position3:
tab_view_pager.setCurrentItem(2);
return true;
}
return false;
}
};
You have to set 0th position item selected for BottomNavigationView.OnNavigationItemSelectedListener. Find Menu bottom_navigation_bar_position1
I used this in my code, please refactor according to your needs
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
Menu menu = navigation.getMenu();
this.onNavigationItemSelected(menu.findItem(R.id.navigation_doc));

Switch activity/fragment with bottom navigation

I created an activity with a bottom navigation bar.
I googled a lot things about it but now I don't know how to handle this exactly.
Before, I just started another activity when the user clicks the bottom navigation but I think it's not good.
How can I switch between the tabs?
Do I have to work with fragments? And what about 'setContentView(int layoutResID)'? How can I do that? I'm confused...
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_dashboard:
startActivity(dashboardActivity);
return true;
case R.id.navigation_notifications:
startActivity(dashboardActivity);
return true;
}
return false;
}
};
Thank you very much for your help - I hope, you understood what I mean.
Activity transition is always expensive and we should switch from one activity to another only when we are switching the context. A fragment is a portion of UI in an activity. Same fragment can be used with multiple activities. Just like activity a fragment has its own lifecycle and setContentView(int layoutResID) can be set to different layout in OnCreate of fragment.
This link explains more on when to use activity or fragment.
Android developer guide on Fragments
Code path tutorial on bottom navigation views.
Please refer to :-
https://github.com/waleedsarwar86/BottomNavigationDemo
and complete explanation in
http://waleedsarwar.com/posts/2016-05-21-three-tabs-bottom-navigation/
You will get a running code with the explanation here.
Bottom Navigation View is a navigation bar introduced in android library to make it easy to switch between views with a single tap. It can although be used for almost any purpose, but is most commonly used to switch between fragments with a single tap. Its use for opening activities is somewhat absurd, since it ignores its most important functionality of switching the views with a single tap. There are many good articles and blogs out there in this regard, one of which is:
https://medium.com/#hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
Hope this solves your doubt..
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = ItemOneFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
// selectedFragment.getChildFragmentManager().beginTransaction();
break;
case R.id.action_item2:
selectedFragment = ItemTwoFragment.newInstance();
FragmentTransaction transactiona = getSupportFragmentManager().beginTransaction();
transactiona.replace(R.id.frame_layout, selectedFragment);
transactiona.commit();
// selectedFragment = ItemThreeFragment.newInstance();
break;
case R.id.action_item3:
// selectedFragment = ItemOneFragment.newInstance();
Intent intent=new Intent(MainView.this, YoutActivityLive.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// selectedFragment = ItemTwoFragment.newInstance();
break;
case R.id.action_item5:
selectedFragment = ItemOneFragment.newInstance();
FragmentTransaction transactionb = getSupportFragmentManager().beginTransaction();
transactionb.replace(R.id.frame_layout, selectedFragment);
transactionb.commit();
// selectedFragment = ItemFiveFragment.newInstance();
break;
}
return true;
}
});

Android - Back button from FragmentActivity to another Fragment

Let's say I have a ListFragment A that is rooted from MainActivity A. User presses a list from List A and go to the FragmentActivity B. FragmentActivity holds 3 tabs of fragments.
So, I want to put an up navigation to the FragmentActivity B, so that it goes back to ListFragment A. How do I go about that?
This is my try, so far no luck:
public class ItemDetailActivity extends FragmentActivity implements ActionBar.TabListener {
...
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
LatestFragment fragment = new LatestFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.pager, fragment).addToBackStack(null)
.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
LatestFragment is the ListFragment A I want to go back to.
However, I got an error that says I have to implement OnLatestSelectedListener because in LatestFragment, I already put an interface to pass values.
What else can I go inside onOptionsItemSelected?
Assuming you started ItemDetailActivity with a standard intent, you should just be able to use back action like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If fragment A is in your main activity then you can also look in to using ActionBar. Back button takes you to the main activity. It is quite easy to implement as well.
I assume your FragmentActivityB has got the back button.
if not you can get it by
getActionBar().setDisplayHomeAsUpEnabled(true);
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="#######" />
Now you can just identify fragments rooted with MainActivity using some flags like ViewNumber(1,2,3).
Implement the method onMenuItemSelected(....) and pass the viewNumber ie; the Fragment Which you want to show.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
//We are implementing this method to respond to the back button on the action bar.
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
Intent i = new Intent(this,MainActivity.class);
i.putExtra("viewNumber",2);
startActivity(i);
break;
}
return true;
}//onMenuItemSelected
In MainActivity get the viewNumber using Bundles, and you can have some method like this to work out and display the desired Fragment.
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new ListFragmentB();
break;
case 2:
fragment = new ListFragmentA();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
}
}
I hope it will solve your problem.

Categories

Resources