I have two activities in my app, FirstActivity and SecondActivity. FirstActivity accesses information from the internet so it takes a few seconds to load initially. I'd like to be able to open SecondActivity and then easily go back to FirstActivity without reloading it.
One of the thoughts I had on how best to accomplish this would be to open SecondActivity on top of FirstActivity, and then remove SecondActivity, when the user wishes to go back to FirstActivity. That way FirstActivity doesn't have to reload because it was loaded the whole time, merely hidden behind SecondActivity.
However I'm not really sure how to best accomplish this task. I couldn't figure out how to open an activity on top of another activity. Any help on how best to do this would be greatly appreciated, thanks!
FirstActivity accesses information from the internet so it takes a few seconds to load initially. I'd like to be able to open SecondActivity and then easily go back to FirstActivity without reloading it.
I recommend that you start with the 2nd activity directly, and from there you begin an async task to download whatever you need from the internet. When the async task is done, you simply switch to the first activity, which uses the things you downloaded. If you have few things to pass over, consider putting the downloaded info as an extra to the activity switch intent.
Alternatively, look into fragments:
http://developer.android.com/guide/components/fragments.html
Have your two current activities be two fragments. For the main activity, set the 2nd fragment as active at the start of the app. When the download is finished, switch to the 1st fragment.
Related
I have several Activities in my application named, MainActivity, ChangeActivity and some others.
MainActivity displays some images from internet and refresh the page after a specific time. ChangeActivity will alter some data in MainActivity, so the images in Main page will be changed accordingly and on Submit will return back to MainActivity.
The application works fine, when it starts. But when I navigate from MainActivity to ChangeActivity, the previous MainActivity remains as it is and again a new Activity starts. As the previous MainActivity is still alive, it will try to auto refresh, eventually after repeating the same procedure for several times, it takes a huge time to load images in Main page.
I am looking for a solution, so I can destroy the previous MainActivities and have only have one, so the images from internet can download faster. Also other activities use the data from MainActivity. So destroying this completely might affect others.
Please do let me know, how I can utilize the resources effectively without affecting the other activities.
You can add finish() right after you start another activity. For example if you are starting activity ChangeActivity from MainActivity
Intent intent = new Intent(MainActivity.this, ChangeActivity.class);
startActivity(intent);
finish();
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();
I have an Android app with multiple activities. The main activity communicates over a network and can launch or dismiss various other activities depending on commands it receives over the network. When an Activity is dismissed I don't want to finish() it, just move it down the stack so it's no longer the top activity. What I really need is a FLAG_ACTIVITY_REORDER_TO_BOTTOM but there is no such thing.
There's an intent flag called FLAG_ACTIVITY_PREVIOUS_IS_TOP and the name implies something like that but I don't understand the description:
"If set and this intent is being used to launch a new activity from an
existing one, the current activity will not be counted as the top
activity for deciding whether the new intent should be delivered to
the top instead of starting a new one. The previous activity will be
used as the top, with the assumption being that the current activity
will finish itself immediately"
Could someone please decode that for me, and if it's not what I want IS there some way to tell an activity to submerge itself below the previous one?
This isn't possible. The activities are stacked and you cant put one back under the other. It sounds like you may want to create a class that extends Android’s android.app.Application.
I found this tutorial online and it looks good. Good luck.
Extending Android's android.app.Application tutorial
You cannot move an activity below a certain activity into the android back Stack. The only way to move a activity in back stack is to open another activity on top of it. You can move an activity on top by creating a single instance of activity using FLAG 'singleTop' in this way your activity will be moved to the top of another activity and only a single instance of activity will be there in stack.
More information about activity back stack and Flags is available here.
Go through this information and all your doubts will get cleared about back stack.
In my app I have situations where I need to get a user back to some activity that preceded(not necessarilly directly) the current one. All of those previous acitivities might need Intent parameters in onCreate.
So, my question is there any easy way to get user back to an activity that might not be the direct previous activity he's been on and is it possible to avoid manual workaround of saving/restoring those previous activities' intent parameters ?
Consider an example: there's a global search-bar that can provide users with suggestions on products; once they hit one of suggested items they get moved on a product-view activity where they can reload this activity with another product - walk through. After a couple of such reloads they might decide to go back to the activity where the search was initiated, but it might not the closest to the current one.
UPD: There also should be a possibility to go back in B activities sequence.
Using startActivityForResult() while loading a new activity and using finish() to close the launched activity on back press can solve your problem.
As Android newbie I started to wonder about the Activity lifecycle. I'm having an Activity that loads a list of Persons and displays them. Upon the click of a Person I want to open another Activity showing the details of that Person. I'm currently doing this by creating an Intent on the "PersonDetailActivity" which I then start. So far so good. On the PersonDetail page I would like to have a menu action to go back to the Person list. I again applied the same technique, meaning an Intent that brings me back to the PersonListActivity.
Now I started to wonder what returning to the PersonListActivity means. Will a new instance get created and will I have to reload the persons that it displays in the list? Can you come back to the same instance, avoiding having to reload the list again? Do you then have to pass a pointer to yourself via the intent to the other Activity?
So when will and Activity be re-instantiated and when will it not. Any hints or suggestions are more than welcome. Maybe there are some patterns to be applied for these back and forth menu actions that I'm not yet aware of.
Thanks,
Vincent
Yes,,. Call finish() in second Activity instead of starting new Activity..
There is basically something called Activity stack which stores all Activities in the order they were started.. so if start new Actvity , that sits on top of the stack and preveous one gets below it.. when you call finish the Activity is poped out..
if you don't want to call finish() correct waht ever you were doing then add flag ACTIVITY_CLEAR_TOP in manifest for the 1st Activity..
Basically if you just call the finish() method on your PersonDetailActivity
PersonDetailActivity.this.finish();
it will activate the onResume() method from the Activity that is on the top of the finished one, which here would be your PersonsActivity. You can specify in your onResume() method what you want to perform when turning back there.