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();
Related
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.
I've written an android application.
So, this is what happens:
I've got a listview and if the user taps on a specific row the following code in invoked to create an activity:
Intent intent = new Intent();
intent.setClassName( getPackageName(), getPackageName() + ".matchView.MatchViewActivity" );
intent.putExtra("leagueId", leagueId );
startActivity( intent );
Then the activity is started and let's suppose the user return back to the list view by tapping the back button
Now he is again in the Listview and can tap the same cell again such that the same activity is started again.
However, the problem is that when I start the activity and I return back with the back button the (now old) activity is not deallocated. It still runs in the background and if the user again taps on the same cell the same acitvity is started again resulting in the same activity running two, three, four and so on times (depending on how often the user taps).
So if the user taps the back button how do I make sure that the activity I started is properly deleted or ended?
Activity will be deleted automatically (when you return back) by system if you are not holding any reference to it.
Better read about activities and back stack
Answering to your question, if you don't intend anymore to use an Activity, the correct way of ending it is by calling finish().
This tells the Android SO you don't want to use it again, but don't expect it to be finished immediately. Android keeps the references to both instances and Activities (this ones are instances too) in memory a certain time after finished so if the user reopens that Activity within a short amount of time, it's recovered much faster.
If you are worried about the state in which the Activity is reopened, you can force it to be restarted every time it's reopened. You might find more references in the Intent reference page, especially the FLAG_ACTIVITY_CLEAR_TOP flag.
You can define in the manifest how an activity is launched based on the "launchMode", and you can choose from one of four launch modes:
"standard"
"singleTop"
"singleTask"
"singleInstance"
It sounds like you want to use, "singleTask" or "singleInstance", which you can read more about under the "android:launchMode" section here:
http://developer.android.com/guide/topics/manifest/activity-element.html
Add finish(); after starting the new activity to delete the current one.
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.
I have an application that implements somewhat like having a top page.
So like, i have this activities:
TopActivity , FirstActivity, SecondActivity, ThirdActivity and FourthActivity.
Each activities has a button that when you press it, it will bring you back ti the TopActivity.
The way I implemented this one is every time that button is pressed, I start an activity, a new TopActivity. So, every time a top activity button is pressed, it always create a new activity. If I also will finish() the TopActivity when it goes to first, second and third, I can not go to TopPAge using back button. Are there itger ways to do this?
Any help is greatly apprieciated.
You can use a different Launch Mode for you activities. The launch modes decides when and how to create a new Activity or reuse a previous one.
The singleTask launch mode seems like it would do the trick for you:
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.
I am trying to close a whole stack of activities using the way described here:
How to kill an application with all its activities?
Namely, each activity starts the other one with startActivityForResult, and in onActivityResult calls finish() to close itself together with the activity it opened.
The problem is that the activities in the task still seem to repaint themselves at least once before they close, and this doesn't look good. After closing the topmost activity one sees all previously opened activities like in a very fast slideshow.
How can one avoid this graphical issue?
EDIT: I need that if the user presses HOME button and then presses the app's icon in launcher, he returns to the current state of the stack, not to the very first activity again. So, from what I understand, with such a requirement I can't finish() activities before starting next ones.
That's native behaviour, intended to aid in user Experience. When an Activity is started with startActivityForResult and then finishes, it will (on devices that allow fancy animations) automatically slide away. That helps people not get surprised by the screen suddenly changing.
You could try starting the Activities without startActivityForResult and handling the passing of data to and from Activities manually, then handle how/when Activities finish() and which Activity they pass back to. You might find you implement something where Activities actually pass forward to the appropriate Activity all the time, rather than back to an Activity on the stack.
Intent intent = new Intent();
intent.setClass(getApplicationContext(),
PhoneListCheckboxAES.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If u give like this when u are starting the next activity then the graphical problems won't occur