I am having 4 (let's say 1,2,3 & 4) fragments. And at a time any one of them will be visible to User. In 2nd fragment I want to do something when user is coming on it.
Now when User navigated to 3rd fragment & hits the back button, I want to run a some code. My problem is onResume is not getting called when user hits the back button & come to 2nd fragment.
I recently bumped into the same problem, I know that its too late, but just in case some one else is looking for this, here's my answer:
Thanks #fasteque for narrowing down my search.
The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.
But if you still want listen to the changes in your activity like which fragment is on top, and trigger events accordingly, you might wanna have a look at FragmentManager.OnBackStackChangedListener
Hope this helps :)
I've had the same problem. If you want to switch from 3rd fragment to 2nd fragment (with the back button or another way) you can call 2nd fragment in the onPause of 3rd fragment.
#Override
public void onPause() {
super.onPause();
Fragment2 fragment2= (Fragment2) getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment2);
if (fragment2!= null) {
//you can call any function from fragment2
fragment2.SomeFunctions();
}
}
Related
I have two Activities and each one has 3-4 Fragments that each replace the other one.
When Activity1,Fragment2 launches the second activity, Activity2,Fragment1 is launched and onBackPressed closes the Activity2 and goes back to Activity1 as normally.
When I'm launching Activity2 from Activity1,Fragment3 though, I want to add Fragment2 to the 2nd activity, which is done via cone in the onCreate method.
However, when the back button is pressed, the fragment disappears for a split second, leaving Activity2 blank on the screen and after that Activity2 is closed, resulting in a visual glitch.
Is there anything I can do to close Activity2 the exact moment (at least at the same visual moment) as when Fragment2 is being closed by the back button?
Thank you.
P.S.: Not adding the Fragment to the backStack seems to close the activity immediately, which is what happens in the first case (expected behavior), but when adding the Fragment to the backStack (as needed for correct operation in the second case) produces the glitch.
Ok, after fighting with the backStack for many days, I decided to skip it, since it does not seem to be working properly, or I'm not understanding it.
The way I did it is on my Activity's onBackPressed i'm checking which fragment is showing and if it's the one that should be the last one (Multiple ones are the "last one") I just finish() the Activity.
#Override
public void onBackPressed() {
MyFragment myFragment = (MyFragment)fragmentManager.findFragmentByTag("MyFragment");
if (myFragment != null && myFragment .isVisible()) {
//if myFragment is visible when the back button is pressed...
finish();
} else {
super.onBackPressed();
}
}
Don't forget to give a name to your fragment in the FragmentManager transaciton. In my case it is "MyFragment".
I have a scenario where I have 2 fragments.
Clicking on a button in the first fragment takes you to the 2nd fragment.
By clicking the "UP" button in the 2nd fragment you'll get navigated back to the first fragment. Unfortunately the OnCreateView() method of the first fragment is not called.
Is there a way to call it? Which methods are called by clicking the "up" button?
It won't be called since 1st fragment is not yet detached from its activity and not destroyed yet. In your case, onResume() callback would be the best place to put your code.
OnCreateView doesnt get called because the fragment A is already created.
Read about the Fragmetn/Activity lifecycle and u will understand that.
OnResume will get called once pressed back from Fragment B, so u can put your logic in that method.
I have an application with two activities. The first contains a tab bar with three fragments in it. Clicking on a button in the fragment loads the second activity.
When the second activity is dismissed, either by the back button or by the Up Navigation, I need to refresh the data in two list views in the main activity's fragments.
What is the best way to do this?
The key is understanding the Activity and Fragment lifecycles. When the first Activity and Fragment return from to the foreground several events are called such as onResume() this is one spot your refresh could occur at.
You need to start your second Activity with startActivityForResult, and in onActivityResult, refresh your fragments.
When your second activity dismissed, the first one brought to front, right? This causes one of the tab fragment to show again. In your fragment you probably have a List Adapters for your list views.
Try this in your fragment:
#Override
public void onResume() {
super.onResume();
mListAdapter.notifyDataSetChanged();
}
I have two fragments,
fragmentA in foreground
now I show fragmentB with FragmentTransaction.add(id, Fagment), (not .replace) so the fragmentA is still alive, with fragmentB on top of it,
now I use back button, here the fragmentB is destroyed, leaving fragmentA visible,
at this moment, how would I know that fragmentA has returned to the "foreground", ie onResume,
note that onResume is not called, due to FragmentTransaction.add(id, Fagment), in other words, fragmentA doesn't go onPause when fragmentB is shown
thank you very much for your help
If you want to know when fragment A becomes visible again, you can first hide it in the fragment transaction that creates fragment B:
fragmentTransaction.add(id, fragmentB).hide(fragmentA).addToBackStack(null);
Then in Fragment A, override onHiddenChanged:
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
// Handle visibility changed. Note this method is called only when the state is changed.
}
When the back button is pressed, the fragment transaction will be reversed and the fragment's state will be changed to visible. One thing you have to watch out for: I've noticed that the hidden state isn't preserved between activity rotation so you'd have to perform your own bookkeeping in onSaveInstanceState. I do something similar to what you're asking since in my case the fragment views are expensive to recreate.
Before doing this though, you might want to consider handling your fragments another way, like with .replace() instead of .add(). If your fragment is completely hidden by the new fragment, then maybe you don't need to keep it around, and you can let the fragment manager bring it back once the user hits the back button. That way, you can just use the normal lifecycle functions like onPause and onResume.
I am a little bit confused about how android's lifecycle works and how to detect the source of function call such like onResume.
Let me state this problem this way. I have an app which contains 5 activities, they will be navigated using a tab activities.
So after switch each tab, I know the onPause() for the current activity and onResume() of the new activity will be called.
Say at activity B, I press the back button, and then I go back to the android's main screen. After I re-open the application, the activity B will be onResume().
What I hope to achieve is that, when the an activities is resumed from main screen, the contents and data will be refreshed. But they should remain unchanged during the tab switch.
Is there any way I could achieve this? How could I know whether an activity is resumed from android main screen or from tab switch?
Thank you!
I don't know how your app logic is set up but you can always have a public static variable somewhere that you can change to hold a tag for the lastScreen.
e.g:
Put this in your main activity for example :
public enum LastScreen {
NONE, TAB1, TAB2 , TAB3 , MAIN_SCREEN
}
public static LastScreen lastScreen = LastScreen.NONE;
and in every tab do the following before you switch :
MainActivity.lastScreen = LastScreen.TAB1;
and in onResume() you can check for it :
if(MainActivity.lastScreen == LastScreen.TAB1)
//do something!
else if (MainActivity.lastScreen == LastScreen.MAIN_ACTIVITY)
//do something else!