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.
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.
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.
In my app there are three activities.
A can open B
B can open C, or return to A, or return to C (if opened from C)
C can open B, or return to A
I have implemented this using various RESULT_CODEs and onActivityResults. However, the stack becomes too large after prolonged use of the app!
I need to clear activities from the stack. Looking at my app's structure, the best clearing method revolves around clearing whatever is the second activity on the stack (assuming A is always the first/bottom activity on the stack). I made a quick painting to make it easier to understand:
Once B is added onto ABC, the previous B (second activity in the stack) is removed. Once C is added onto ACB, the previous C (second activity in the stack) is removed.
I can think of two methods that do what I describe:
clear whatever is the second activity in the stack
remove any activities in the stack that are the same as the one that I am creating (C creates B, so the other B needs to be cleared)
However, I have not found a way to implement this in code. I have tried using Intent.FLAG_ACTIVITY_CLEAR_TOP, but that clears everything in the stack except for A, before adding the new activity at the top.
So my question: How can I implement method 1 or method 2? (Does not matter which one)
Here is an example of me clearing all in the stack but A:
Intent newActivity = new Intent(this, B.class);
newActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(newActivity, MY_REQUEST_CODE);
I am also open to alternative methods. Maybe using FLAG_ACTIVITY_NO_HISTORY somehow?
So it seems like you would like to keep one instance of B activity and C activity. This can be done with adding an XML attribute in your Manifest.xml
<activity android:name="B" android:launchMode="singleTask" </activity>
"singleTask" - 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.
Refer to here for more information.
I have occurred the same problem , to kill some specific activity .. my solution was to implement StackActivityManager used for managing activity :
Each activity was saved on static list which contains all activity of my application
The saving of activity was done the mother class base activity
Each activity have a static and unique tag used later to indicate which activity will be remove from the stack of activity
The launch mode is singleTask to avoid duplicate activity in the stack of activity.
try to do method:
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
//The finish(); method, destroy the current activity.
finish();
after call the next activity, then the current activity destroys.
I hope that it works. :)
remove this line
newActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Try adding in your code-
Intent newActivity = new Intent(this, B.class);
newActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivityForResult(newActivity, MY_REQUEST_CODE);
Note: What Cory Charlton wrote in his comment about Fragments is probably the best solution for anyone having similar problems. I recommend trying that route instead of my own solution. The only reason I went with my own solution is because I needed a quick solution, and had not the time to learn how to implement Fragments before a deadline.
I ended up solving the problem by having the A activity always start the C activity, instead of the B activity. The B activity no longer creates a C activity, it instead finishes in order to go back to C.
This way the stack never gets bigger than 3.
And to provide some code, I had to add this before finishing in the B activity, since it now no longer is passing an object, but is returning it:
setResult(RESULT_OK, getIntent().putExtra(KEY_MY_OBJECT, myObject));
Learning how to make android apps and I did this tut. Summary of the tut is here:
http://sketchytech.blogspot.com/2012/10/android-simple-user-interface-activity.html
I'm trying to figure out how intents work. In the tut you create an Intent called intent, and in DisplayMessageActivity.java it creates an Intent called intent by calling "getIntent()".
Does the "getIntent()" function (or method (I'm most familiar with C)) just return the most recently created intent? Can there only be one intent at a time?
Thnks in advance for any responses!
All Activities are started by either the startActivity(Intent) or the startActivityForResult(Intent, int) methods. The intent tells the Activity everything it needs to know to display the correct information at launch. getIntent(), when called in an Activity, gives you a reference to the Intent which was used to launch this Activity.
getIntent() method gets the intent that called this activity.there can be more than one intent but you have only one intent visible at one time (since only one activity is visible at one time)
An Activity is typically created through an Intent. Assuming you are in your first activity:
Intent intent = new Intent(MyFancyActivity.class, Intent.ACTION_VIEW);
startActivity(intent);
This launches a new MyFancyActivity instance. From MyFancyActivity, you can retrieve the intent which lead to that instance creation. That is, the getIntent() method:
// this is the intent created in your first activity
Intent i = getIntent();
There are two primary forms of intents you will use.
Explicit Intents have specified a component (via
setComponent(ComponentName) or setClass(Context, Class)), which
provides the exact class to be run. Often these will not include any
other information, simply being a way for an application to launch
various internal activities it has as the user interacts with the
application. Implicit Intents have not specified a component; instead,
they must include enough information for the system to determine which
of the available components is best to run for that intent.
An Intent is a data class which holds information for an Activity that is about to be started. An Activity is a manager or controller for the view which is currently displayed on the screen.
Activities in the system are managed as an activity stack. When a new
activity is started, it is placed on the top of the stack and becomes
the running activity -- the previous activity always remains below it
in the stack, and will not come to the foreground again until the new
activity exits.