This one is weird. I'm implementing breadcrumb navigation on a older app and I'm not at the point where it's ready to refactor the views. Unfortunately every new navigation opens a new activity and sometimes the new instances of the same activity. So my breadcrumb navigations basically look like this:
Activity A > Activity B > Activity C > Activity C > Activity C > Activity C > Activity D
So if I'm on Activity C and I need to go back to Activity B I can just set the Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP for the intent and navigate back no problem. But if I need to navigation from the 4 instance of Activity C to the 2nd instance of Activity C I'm not able to use any intent flags to make that happen. My app is already making use of the back button so I cannot override that to utilize here. Does anyone have any ideas as to navigation among the multiple instance of Activity C?
Fortunately all these app do inherit from the same parent app which extends FragmentActivity if that is helpful.
To go back one item in your breadcrumb you don't have to use the BACK button, you can just call finish(), which will finish the current Activity and go back to the previous one in the stack.
To go back to a specific Activity, you can use startActivity() with flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP, as you have already indicated in your question.
Anything else is not possible. See my answer to a related question here:
https://stackoverflow.com/a/39104837/769265
Related
1) When I click an item in a RecyclerView in Activity A, it takes me to another Activity B, when I navigate back from that Activity B back to A, A somehow calls onStart/onCreate and the RecyclerView data that I was scrolled at all changes. How to prevent this?
2) Say I have an Activity A and Activity B where I have buttons in both that navigate to Activity C. When I go back from Activity C, I want to determine which Activity I came from, A or B, to navigate back to. In AndroidManifest, I can only put ONE ParentActivity, so I don't know how to go about doing this.
onStart() will be called when resume activity A from B. You have to save the last scroll position of recyclerview yourself. you can refer to this answer
Not sure how you implement the navigation to activity C. If you use startActivityForResult() in A and B then you can override onActivityResult() in A and B to get the data from C
1) When I click an item in a RecyclerView in Activity A, it takes me
to another Activity B, when I navigate back from that Activity B back
to A, A somehow calls onStart/onCreate and the RecyclerView data that
I was scrolled at all changes. How to prevent this?
Instead of startActivity() start Activity B using startActivityForResult(). You can refer to this SO to implement it.
when I navigate back from that Activity B back to A, A somehow calls onStart/onCreate
onStart() will be called because Activity A entered onStop() after launching Activity B using startActivity()
onCreate() might be called if OS is under memory pressure and destroys your activity to relinquish some memory. It will recreate it before its being displayed back to user.
Refer to Activity lifecycle document.
Say I have an Activity A and Activity B where I have buttons in both
that navigate to Activity C. When I go back from Activity C, I want to
determine which Activity I came from, A or B, to navigate back to.
You can use startActivityForResult() here too.
Note: You need to be aware of potential issue with onStartActivity() getting called pre-maturely. Refer to this SO
Technically I have a MainActivity "A" then then I'll go to next activity which is activity "B" then there is the other activity which is Activity "C"
But when I use finish, the activity B and C returns to each other instead backing to the main activity
When you route from Activity A to B then c, and you pressing back from C to B and A. You don't need to use finish() anywhere. Because android maintain stack for back button. It automatically push all activities and pop activities when back.
Refer this : https://developer.android.com/guide/components/tasks-and-back-stack.html
The loading of JSON data part should be done in onCreate() method,because you dont want to re-load the data.override the onbackpressed() method of activity on the some action (like button click) of the A and B activity Or use finish.
Consider this:
Activity A
Activity B
Activity C
Activity A is started when clicking on launcher icon. Activity B is started by activity A - nothing special. But Activity C should be started ONLY by app itself (not on click or something similar ), like broadcast.
THE PROBLEM:
When Activity C is started by app automatically, and when i press HOME button on this activity, and when i click on lanuncher icon (to show up Main Activity - A ), it shows me activity C. If i press BACK button on this activity, then i can go to activity A, with no problem.
How to solve that issue? Killing activity C, on HOME button press, or is some other way to do this?
In manifest of your Activity A put android:launchMode="singleTask" android:clearTaskOnLaunch="true" and in rest of the Activities put android:clearTaskOnLaunch="false", This will solve your problem. Hope this will help you.
Just add android:noHistory="true" to <activity> element of Activity C in your AndroidManifest.xml.
Setting true for android:noHistory will make the activity not leaving a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
In my application i am using tabhost. And to control different activities i am using activity group. I have Activity A from which i am going to activity B. Activity B contains Edit Text, Spinner, Button etc. Now when i scroll the Activity B and press the device Back Button than it does not go to the previous Activity. It goes out of application.
Kindly suggest the answer.
This is the way the Activity lifecycle works for Activity Group. Try using fragments instead of activity group with your tabhost to get your movements. Also, please edit your question title to more accurately reflect the actual question.
I have an app that circles around the main activity (a main menu). In each other app there is an option menu item that directs to this activity.
At first, I always started a new main activity when this item was selected. Using the intent bundle, I did tell the main activity that some initializations I do on a fresh start were not necessary.
However, I didn't quite like the overall behavior. I stumbled upon android:launchMode="SingleTask" and this seemed to help: now I don't recreate my main menu activity all the time; also, if I press the "back" button I come back straight to the home screen. This feels quite nicely like a proper "main" menu.
My problem now is this: if I run another activity of my app, press home button and then reopen my app (e.g. using "last apps"), then I don't go back to the last activity, but to the main one. The other activity is destroyed.
Any ideas how I can implement the behavior of SingleTask without only being able to return to one activity?
If your other activities are declared normally with activity defaults in Android, then going back to your app should take you to the same activity where you left off (using the hardware home button)
However remember that the Android system kills applications when it requires system resources. So your app may have been killed when you went to the other application. Then when you get back to your app, the default launcher activity will be restarted, which is your Menu activity.
To get back to the main activity from any activity, do this:
public static void goHome(Context context) {
final Intent intent = new Intent(context, HomeActivity.class); //give name of your main activity class here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
That will clear the activity stack and get you back to your main activity. As you declared singleTop, it will bring the existing main activity to the foreground. The flag Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all activities in the stack on top of the main activity. (I am assuming you are within the same application).
Now, all your other activities only need to include a button whose click listener invokes the method goHome();
From your main activity, if you press the hardware back button, it should exit your app.
Why not call finish() on the activities that were created by the main activity? This way you return to the main activity, without creating a new one...
I think you should save the state of you activity before starting another activity, and then resume your activity whenever you come back on last activity.
see Activity Life cycle from Android
http://developer.android.com/guide/topics/fundamentals/activities.html