Which one to use NavUtils.navigateUpFromSameTask() vs. onBackPressed()
Solution c is the correct option. First, though, an explanation of the problem with solution a.
There is absolutely no point in having two back buttons in your Activity. Furthermore, option a actually breaks the up button. The point of the up button is to provide a way for users to stay within your app when they have landed in your app from an outside source. For example, if you land on activity B from an outside activity C and if you are using your option a, then pressing "up" in activity B will result in activity C being shown. The behavior you would want would be for activity A to be shown.
As you can see, solution b is on the right track. You definitely want to go up to A and not back to C. However, simply storing the state in onSaveInstanceState
will not cause the state to be retained. This is because onSaveInstanceState
only gets called if your application may be killed by the system. It is not guaranteed to be called if your application was destroyed manually, and it certainly won't be called when a new instance of your Activity is created. If the Intent starts a new activity, then it will not have its state restored from the other activity.
There solution, then, is that you must write anything persistent to a shared preference file (or a custom persistent alternative). When doing this you can guarantee that all instances of an Activity share the same state across multiple tasks so long as their onResume (or wherever you restore state) is called. OR:
If you know exactly how you want your navigation to work, you can avoid writing everything to persistent state by using a combination of Intent flags and Activity task affinities. If you want to use the same activity as up even if you navigate into the application from an outside source, then you can leave your Activity A's affinity as default (linked to the application) and use something like Intent.FLAG_ACTIVITY_CLEAR_TOP.
Personally, I'd try the Intent flag approach first and failing that fall back to writing the state persistently. You just don't really want scroll location sitting on persistent storage if you can avoid it..
How can I go to previous activity with the fragment the activity was showing
When you return from activity B to activity A you recreate your fragments, so it will always show your fragment1 instead of your desired fragment2.
Your problem is, that you trust on savedInstanceState to decide if your fragments should be recreated or not. However, onSaveInstanceState()
is only called, when the activity gets destroyed, which will not instantly happen when you navigate to the next activity.
You should query the FragmentManager
in your onCreate()
method of activity A, if fragments are already added, otherwise you do your initial fragment creation. In order to do so, I would add an TAG to better find your fragments.
private static final String FRAGMENT_TAG_CONVERSATION = "frag_conv";
...
fragmentManager.beginTransaction().replace(R.id.content_frame, conv, FRAGMENT_TAG_CONVERSATION).commit();
You can then check, if you already added the fragment:
if(fragmentManager.findFragmentByTag(FRAGMENT_TAG_CONVERSATION) == null))
// only add it here
Navigating Up to an activity that expects extras
If you're going to be returning from C back to B your activity will be created again, if you use standard launch mode. onSaveInstanceState will not (reliably) work.
Declare the launch mode of your activity B as:
android:launchMode="singleTop"
in your AndroidManifest.xml, if you want to return to your activity. See docu and an explanation here.
Related Topics
Difference Between Surfaceview and View
Keep a Service Running Even When Phone Is Asleep
How to Know My Android Application Has Been Upgraded in Order to Reset an Alarm
Error :: Duplicate Files During Packaging of APK
Android: Retrieve Contact Name from Phone Number
Problems with Gridview Inside Scrollview in Android
How Does the Getview() Method Work When Creating Your Own Custom Adapter
Android Character by Character Display Text Animation
Error: No Toolchains Found in the Ndk Toolchains Folder for Abi with Prefix: Llvm
Disconnect a Bluetooth Socket in Android
Android - Hold Button to Repeat Action
"Canvas: Trying to Draw Too Large Bitmap" When Android N Display Size Set Larger Than Small
How to Retrieve the Logged in Google Account on Android Phones
Ongloballayoutlistener: Deprecation and Compatibility
Answer Incoming Call Using Android.Telecom and Incallservice
How to Change the Color of a Switchcompat from Appcompat Library