Let's say I have Activity A and Activity B.
In A, I have a button which when clicked runs the following code
startActivity(new Intent(this, ActivityB.class));
In B, I have a button which when clicked, runs the following code
startActivity(new Intent(this, ActivityA.class));
So when the app is started and the user does the following:
Clicks on button in Activity A (he goes to Activity B)
Clicks on button in Activity B (he comes back to Activity A)
Again clicks on button in Activity A (goes to Activity B again)
In this case, do the Activities A and B hog the memory since they get started each time or is Android smart enough to know that an activity has already started and simply needs to be put on top of the stack?
So in other words, if memory does indeed gets clogged up, is there something like:
if(activity has already started)
{
doNotStartActivityButSimplyPutItAtTop();
}
else
{
startActivity(new Intent.....);
}
yes, your assumption is right android is capable of managing these activities and they are maintains proper stack for it.
if you are having only two activities you can use singletop launcher mode so that only only two activities remain in stack.
The android system may release the resources from previous activity in the stack. To avoid losing the current state of the activity you may have to save it and then restore when the activity is recreated. See Recreating an Activity.
Related
I am navigating from Activity A to Activity B without finishing Activity A (because i want to go back on it and have some variables values).
In Activity B I launch camera and save captured image but the problem is, after capture camera the Activity A is re-create and resumed, causing re initialization of my variables.
How to stop it?
Note: The problem occurs only in Nougat Version.
The thing you want to know here is about the complete activity life cycle. Basically to summerize them they are
OnCreate, - called when activity is created
OnStart, - called when activity starts
OnREsume, - when activity gets back
OnPause, - when activity is overlapped
OnStop, - when activity is closes
and onDestroy - when finish() is called.
Though you have not mentioned how you started an activity and got back to the same activity, the correct way to get back to previous activity is by calling
finish()
on camera activity.
I guess you have got back to activity A by using
Intent i= new Intent(this, ActvityA.class)
startActivity(i)
which is the the correct way to do.
Just call the finish() when you want to get back to Activity A from B (Here B is in top of A).
Additional
If you want to pass data from activity B to A, just put something called as Intent Extra or Bundle
Activity A is create again and resume, so my variables are initialize again. How to stop it.
Yes that is normal. First rule with Android programming is that your activity can be killed at any time. So to not loose your variables you have to save them at the right moment.
You would do that to override onSaveInstanceState() putting your variables in the bundle.
Then you can retrieve them in onCreate() from the function parameter.
This may be a noob question, but I've been searching for some explanation about it and wasn't able to find.
Well I have the A.class which is initiating an activity (A).
The user click in a button and we go to B.class, which also initiates a layout and I'm sending a putExtra("key",value) to the activity (B).
I receive it and works perfect!
Then I want to send a putExtra("key",value) back again to activity (A), but the user clicked in another button I started the C.class to do a background task of what he needs.
When the user goes back to activity (A), the getIntent().getExtras() is empty.
So my question is, changing classes (threads) or activities can mess your bundle?
Is there a way to prevent it?
I think you're very confused. The intent returned by getIntent is the Intent that started your activity. It will never change. If you have an Activity A that wants to start Activity B and get a result, Activity A must call startActivityForResult to start B, and B must set a new intent to be returned, call setResult, and then finish. THen onActivityResult in Activity A will be called, and passed the result set by B. The getIntent() will not return any results.
I have been trying to figure out why my Intent would not transfer string data from one activity to another activity? I seems I had set launchMode = singleTask in the manifest folder and when I changed launchMode to standard the Intent code worked as expected.
The MainActivity is the first activity in the stack I am guessing that I made the setting a number of months ago to try and prevent the user from using the back button to navigate back to the password log in page. (MainActivity)
I kind of get the Back Stack idea but WHY would this setting inhibit the intent from transferring data. my test for transfer was a System.out.println statement?
Suppose you have activities A and B. A is the one with android:launchMode="singleTask". A starts B. B then starts A, causing the existing instance of A to return to the foreground.
In that case, A is called with onNewIntent(), and that Intent will have the extras from B.
onCreate() is only called when an activity is created.
In my application when i click the back button it passes through all the activities that i open them previously , i used the public void onBackPressed() method to make the back button back to the activity that i want as follow
public void onBackPressed()
{
startActivity(new Intent("com.MyDiet.Main"));
Tracker.this.finish();
}
is that true and safe way to code the back button ? how i can prevent the application from passing through all the previous opened activities when the back button is pressed ? and how i can make the application exit when i click the back button in the main activity?
In your application, and in ALL android applications, unless it's critical not to pass through unneeded steps (such as login if you're already logged in), it's VERY important not to override Android standard behaviour. Users normally complain about Android apps not having a common behaviour or style guideline.
Anyway, yeah, you can just override onBackPressed on all your activities and do whatever you want. But just don't.
This approach isn't good, because you're polluting the activity stack of your application
Example:
current stack: MainAct -> Act2 -> Act3 (we're in activity 3)
With your code above, when you press back, the stack now looks as follows:
MainAct -> Act2 -> MainAct
Because you ended Act3 and launched a NEW main activity, which may be not what you wanted.
To achieve what you want (Get back to main when the current activity is over) you need to work on the intermediate activities: In the example above, when from Act2 you call startActivity("Act3"), you should call "this.finish()". Therefore you don't have to override "onBackPressed()" of activity 3: simply the default value will terminate Act3 and the next activity in the stack will be MainAct
MainAct -> A2 (A2 launches A3 and then calls this.finish())
MainAct -> A3 (user now press back)
MainAct (the mainactivity is now on top)
To summarize, you don't have to override onBackPressed, you just have to correctly manage the lifecycle of the activity between the main one and the current one.
Generally speaking it's not recommended to make things work like user doesn't expect and that is considered as very bad practice. That's why it is not good to start activity from overriden onBackPressed().
When user press back by default activity will finish and one from back stack will be displayed. You as developer will (by writing code) decide which one is that. You can do it this way but later (or for somebody else) it will be a bit messy and difficult to find this unusual place for code which is starting other activity.
So..It would be useful to read about activity lifecycle and back stack to get impression how it works and understand terminology better.
If you want one of your activity not to stay on back stack you can add in manifest file file android:noHistory="true" for that activity.
Same thing you can achieve from code by adding appropriate flag to intent when you start activity: Intent.FLAG_ACTIVITY_NO_HISTORY
When user "go away" from ActivityOne (started using intent or defined in manifest like described), to new ActivityTwo and then press back, it will not go to ActivityOne because it will not be on back stack. Using this you can precisely control navigation through your activities.
More flags are available for use, but I guess this is what you wanted to achieve.
Hope you will find my answer useful. Cheers..
You can use a lot of tricks to exit your complet application for my part i use this start a intent and then from the androidmanifest i choose the category to home and than close the current activity such would be your mainactivity !
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
My application shows an alert that the user must respond to before continuing to do other things. I'm trying to figure out the best way to implement this. Using an Activity for the alert isn't quite working.
In my current implementation, the alert is activity (A). When another activity from the same package is started and onStop is called, it starts itself again using FLAG_ACTIVITY_REORDER_TO_FRONT so that it's always at the top of the stack. This works as described, unless Activity A uses Theme.Dialog or Theme.Translucent.
Modified log:
Activity A created
Activity A started
Activity A resumed
Activity A paused
Activity B created
Activity B started
Activity B resumed
Activity B gains window focus
Activity A stopped
Top activity in stack is Activity B, so Activity A relaunches itself
Activity B paused
Activity A started
Activity A resumed
The top activity in the stack should be Activity A, however Activity B remains in the foreground.
Another implementation detail: my application is not for a phone, so I'm not concerned with a back button finishing the activity or interactions with other apps. Still, I agree that on principle I should prevent such problems anyway, so in my code I check whether the activity that has come in front is from the same package (i.e. from our code base). This should work around the theoretical problem of interfering with other apps.
Is there a way to bring Activity A into focus? I understand that this is unusual behavior, but it is necessary for Activity A to remain in the foreground until it is deliberately finished.
I'm also open to suggestions about a completely different and better approach!
FWIW, I'm running 2.2.
(Cross-posted from http://groups.google.com/group/android-developers/browse_thread/thread/d46fd7d59abe15a0, where we got no response.)
You can't do this. Please don't do this. The activity at the top of the stack is the one that has input focus. What you are trying to do fundamentally breaks the user interaction that is supposed to happen.
What you are doing is generally considered by the platform to be an abuse of it, and Android has increasingly been doing things to prevent applications like this from causing harm.
Well, here's what I had in mind:
public class ActivityA extends Activity
{
...
public void onStop() {
super.onStop();
finish();
Intent i = new Intent();
i.setClass(getApplicationContext(), ActivityA.class);
startActivity(i);
}
}
ActivityA is finished in onStop() and started again right away. You might have to check issues regarding device rotation, but this approach should work.
Having window focus means that activity B is still in its visible lifetime, since it has on top the activity A which has a translucent bg or is dialog-like.
Having the window focus doesn't mean that activity B is on the foreground. They are different things.
If you don't want this, then don't use those two themes.