I know you might think that this question have been asked a lot, but I've been looking into different cases, all cases they just want to close all activities when logged out. Here is my scenario:
1) Start the app with Splash Screen then I used finish()
//I set this activity as MAIN so it's first to open - that is why I cannot setFlags(FLAG_ACTIVITY_CLEAR_TOP) because it basically goes back to MAIN
2) Introduction Pager(4 sliding pages) with Login and Signup buttons
3) Login and signup buttons direct you to corresponding activities
// I didn't use finish in introduction pages because I want the user to have the ability to go back to introduction if for example pressed signup by mistake
4) after performing login/signup the user will be directed to the homepage(Here I used finish to kill login/signup activities because I won't need them anymore)
So all previous activities are now finished except for the introduction pager, when the user press back the app will finish homepage and go back to introduction, but I don't want that! I just want the app to quit, I don't want the introduction pager to be running on the background. How do I finish introduction pager when onCreate method is called in homepage?
I have another related question I didn't want to post another thread for it: how to get the previous activity? like I don't want to use the action bar to navigate, I created a button (<- Back) and when I press it I want to bring the previous activity, how do I get the previous activity?
First of all make your IntroductionActivity a single istance in the manifest like this: launchMode = "singleInstance"
There are many ways you can do this, the fastest, I guess, is to use a static field that refers to that activity. Add this in your IntroductionActivity:
public static Activity mActivity;
onCreate(){
this.mActivity = this;
}
Then when you reach the homepage you can do this:
onCreate(){
IntroductionActivity.mActivity.finish();
}
Use finish(); after you start the next activity
Example:
Intent itr=new Intent("com.example.splash.Second");
startActivity(itr);
finish();
Related
I have an app with a main menu at bottom; I can't figure out how should I manage the activity stack, because every button opens an activity, and each activity can start more activities, and i was looking for a management in the style of the current Instagram's app. It looks like (in the Instagram app) that every activity started by each button in the bottom menu opens a new activity stack, but when you press back button, it navigates in the reverse order you called every activity.
Sorry for my bad explanation, i hope that you can understand my aim.
You could check out Instagram Android app to figure out what is my goal.
My current implementation uses a MainActivity with a Fragment for the first menu button (Qui in giro->"Nearby"), but i probably should change this approach.
Thanks.
1) Firstly you will have to decide what will be your main activity.
2) Now use this main activity like as star topology. Means this main activity will be center activity.
3) Use to finish() method for finish back stack before receiving at this center activity.
4) After it if you open another side of your main activity than you will see only new back stack will be displayed after pressing back button for receiving at main activity again.
The official Dev Guide of Tasks and Back Stack says, activities can be instantiated multiple times, and Home Activity is taken as an example
So I tried it out as the graph illustrates:
Launch Activity 2
Press Home button
Launch Activity 1
Press Back button (so I return to Home screen)
Press Back button again
But I did not go back to Activity 1. Thus, it seems that Home Activity has not been instantiated multiple times. Is it so? If so, how is it kept in a Back Stack?
EDIT: Sorry, I should've clarified earlier that I didn't write any codes to test it. All I've done is just launching applications on favorites tray. I'd better go to read the source code and search for the behavior of Home Activity.
Anyway, I don't think Home Activity is a good example here to illustrate multiple instances.
Your issue might be that you might have called finish() in your Activity2. Or, the OS clears up the 2nd Activity before you return back to it. The behavior you are trying to attain on your own has no guarantees. You can't force an Activity to keep running so that you can return back to it.
I am new in android and I have total 6-7 activities in my application. I want to know how can I manage my activities properly means when I move to the A->B->C->D like that. Then how can I move that the stack of these activities not created.
On moving from one activity to the other I am using the below code:
Intent intent=new Intent(current.this,next.class);
startActivityForResult(intent, 0);
And now if I want to move back on the earlier activity I used the code as:
Intent start = new Intent(current.this,next.class);
startActivity(start);
finishActivity(0);
Is there a special reason that you don't want to use the activity stack and let the activities handle themselves?
The Android system has done a very good job with the activity lifecycle. It allows you to start an Activity from different places without confusing the user because the back button will bring the user back to a different activity.
If you don't have a very good reason to not use the Android guideline try to stick to the way the system is doing it. Every other thing will only give you problems.
You are starting activities for a result but how I understand you you will never return to them.
You can start an Activity and after that just finish the current Activity. That way the activity will not be put on the back stack. Now you need to listen for back button pushes and create the activities that you want to bring the user to.
If you want to move from Activity A to D like going to the start/home screen of you app you do the following:
Intent goBackToA = new Intent(context, StdActivity.class);
goBackToA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goBackToA);
The flag FLAG_ACTIVITY_CLEAR_TOP will tell the system that if the backstack contains an instance of the Activity this activity will be shown and all activity that are between the current activity and the target activity are removed from the backstack. This allows you to go back to a home activity without creating huge loops that the user can move through with the back button.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapear.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapeear or you can press Back button to see the previous Activity .
whenever you want to navigate from one class to another use this code, may be this help you to navigate the Activity,
Intent nextpage = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(nextpage);
this.finish();
Here i have a few activities that consist different menus in my app..
The problem is that i want to add a are you sure popup box to exit the current menu and return back but calling finish() method on the click event of yes button of popup box causes all activities to terminate and app exits...
I want to make a way to terminate only the foreground activity and
return to last activity programatically (i.e without using back key)
Can u post some source code regarding how you start you new activities? Are you starting multiple activities at all? finish() method only finishes the current activity and not the entire stack of activities, thus the system automatically brings to front the previous activity from the stack. I can't understand your question please provide some further details.
I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.