Back Navigation to last Fragment - android

I have two Fragments in my ViewPager as Tabs. These share a toolbar. You get to the SettingsActivity by clicking an Icon on the toolbar.
So you can access the SettingsActivity while either Fragment A is the visible one, or Fragment B is the visible one.
When I navigate back from the SettingsActivity to the fragments, by clicking the Up Button in the toolbar (actionBar.setDisplayHomeAsUpEnabled(true);), I want to have the fragment visible, that I had visible when I accessed SettingsActivity.
Without any special code (= current state) it seems to always return to the first Fragment in the ViewPager (i.e. the most left one).
Parent Activity for the Up Navigation is MainActivity, where I have the ViewPager with the 2 Fragments as Tabs.

Use the state saving callback to save the state of your MainActivity:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("SELETED_TAB_INDEX", mSelectedTabIndex);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) { // so the activity is being restarted
pager.setCurrentItem(savedInstanceState.getInt("SELETED_TAB_INDEX"));
}
}

save your fragment to backstack. This way, back button for fragments would work same as it runs for activities.
Here: http://developer.android.com/training/implementing-navigation/temporal.html

Related

Android - Remove back button from action bar for a particular fragment

I have an app (Java) which loads a fragment (Fragment A). This fragment has a recyclerView which in turn loads a second fragment (Fragment B). I want Fragment B to have a back arrow that can return to Fragment A, but I don't want the back arrow in Fragment A.
I added this code to the Main Activity:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
But this causes the back arrow to appear in every fragment.
How can I have the back arrow appear only on second level fragments (like B)?
you can hide the button on the wanted fragment like this
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar(). setDisplayHomeAsUpEnabled(false);
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar(). setDisplayHomeAsUpEnabled(true);
}

How can I save fragment state when the user navigates between fragments in a viewpager?

I'm making an Android App, that uses BottomNavigationViewEx to have a Bottom Navigation widget with 5 sections/fragments, I manage them using a viewpager, but one of this fragment (fragment #3) also uses a tab layout to nest another 2 fragments and I need to keep the selected tab when the user navigates to other fragment using the BottomNavigation icons.
The problem is that I need save the state of the fragment #3 (juts to keep it simple, I call them in this post fragment #), that is the fragment that holds the tablayout.
Inside fragment #3 I'm calling:
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("currentDirectoryFragmentId",tabLayout!!.selectedTabPosition)
}
but the method is never being called, and makes sense, because I really never destroy the parent activity, but onDestroy() is being called inside each fragment correctly.
So, How can I save the state of a fragment when the user navigates between fragments that are children of a same activity?
As stated in the comments. You can accomplish this by using a variable inside the parent activity and referring to and setting this variable inside the fragments' onPause() & onResume() methods.
Inside Parent Activity
public static int position = -1;
Inside Child Fragment
#Override
public void onPause() {
super.onPause();
MainActivity.position = viewPager.getCurrentItem();
}
#Override
public void onResume() {
viewPager.setCurrentItem(MainActivity.position);
super.onResume();
}

Is it possible to keep all fragments in running state on using BottomNavigationView, even those which are not visible on the screen?

I am trying to use BottomNavigatioView in my MainActivity and when on clicking a tab in the bottom navigation, the new fragment replaces the previous one, which takes the previous one to the onDestroyView state.
How can we make all the fragments in the onResume state, regardless of whether they are or not visible on the FrameLayout.
You can save the state of fragment and can restore it on onCreateView()
Bundle savedState;
public onCreateView(...){
..........
if(savedState != null){
String hello = savedState.getString("hello");
}
..........
}
#Override
public void onDestroyView(){
super.onDestroyView();
savedState.putString("hello", "Say hello");
}

Android up button with Viewpager: go back to the previously selected fragment

I'm having some problems with navigation and the up button of the activity. I have two tabs implemented with Viewpager in one activity and then another activity which is loaded from the previous one. When the user clicks the phone's back button or the activity's up button, I want to go back to the previously selected tab and fragment of the first activity.
So far I've been able to do it for the back button, with onSaveInstanceState and the following code:
#Override
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putInt("currentPage", mViewPager.getCurrentItem());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
int page = savedInstanceState.getInt("currentPage", 0);
mViewPager.setCurrentItem(page);
getSupportActionBar().setSelectedNavigationItem(page);
}
}
Why is this not working for the up button? What do I have to do?
Any help is appreciated and sorry if this is an issue easy to solve, but I'm rather new to the Android programming.
I finally found the solution at Returning from an activity using navigateUpFromSameTask(). savedInstanceState was null when the activity was recreated. To avoid so, the launch mode of the activity has to be declared as singleTop in the Android manifest.

Android ViewPager FragmentAdapter

In my application i have a FragmentPager. Now each Fragment has a next button with which i navigate to the next fragment. Via the next button i know that the user is navigation away from the view. But how do i know if the user has clicked on the tabs. Will the
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
function be called when a user presses a different tab ? Can i save the state and restore it on the
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null)
&& savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
will this work ? What other way can i know that the user has clicked on the different tab ?
Kind Regards
First of all, are you using ActionBarSherlock for you actionbar/tabs? I recommend that you do since you'll get a cross-version way of working with the actionbar.
In any case, you should add a listener to each tab before adding it to the actionbar. With the listener implemented you know when a tab has been selected, reselected and unselected
I'm not sure about when the onSaveInstanceState is called (try it using the debugger!), but with the listener implemented you'll get a fool-proof way of knowing what goes on with your tabs.

Categories

Resources