Persist data sent by Intent between Activities in Android - android

So i have AppCompatActivity A, B and C. From A, i start activity B, passing an Intent I0. In B´s onCreate i get I0 and initialize the UI based on that data. Now i open C (from B). When i press the app:navigationIcon back button of the android.support.v7.widget.Toolbar from C, B's onCreate is called again, but now the Intent I0 does not contain the data from getStringExtra("data") anymore.
I have tried to use onSaveInstanceState and onRestoreInstanceState as mentioned elsewhere, but onRestoreInstanceState is not called, and the savedInstanceState Bundle is null in onCreate.
So i'm wondering if there is a way to preserve either the UI state of Activity B (not destroy it?) or the Intent I0's extra data?
I've created an Example on Github

Thanks to #pskink for suggesting to set android:launchMode="singleTop" on Activity B. This solved the problem for me in that B is not recreated with an invalid Intent.

Related

send intent to an already running activity

Main Activity has started activity A and A has started activity B, which is currently visible on the screen: Main Activity -> A -> B.
Now Main Activity receives a broadcast intent and it needs to pass new information from it via A to B without restarting A or B, i.e., the state of A and B should remain as is. For example, if user has written someting to EditText in B, that text should not be lost.
Question: what Intent flags should be used when MainActivity sends intent containing the new information to A and A to B in order to achieve the above described behavior?
Use the activity.startActivity(Intent) method in your Activity B to call activity A ‘s “newIntent(Intent)” method, do with what you need to then :) and maybe make sure in the manifest file the activity is set to SingleInstance
Rather than figthing with intents and launch modes, I decided to save the necessary state of A and B at their onPause() method to a companion object of MainActivity and then restore the state from there at onResume() method of A and B.

getIntent().getStringExtra() eventually returning null

So in Activity A I pass some data to activity B through an intent. Ok, everything is fine and getStringExtra returns what I expect. Then from activity B, I pass the same data to activity C. Then, when I hit the back button in the toolbar (because of getSupportActionBar().setDisplayHomeAsUpEnabled(true)), the getStringExtra in activity B is now null.
So the flow is A (passes a string)-> B(passes the same string) -> C (back button in toolbar) -> B and now the variable passes from A to B is null. How can I fix that?
Mark this a rule : Whenever you are using the toolbar back button, you should take care of specifying the launchMode of the parent activity.
In your case, what happens after you press back button in Activity C, depends on what launchMode have you specified for your activity B.
If you have't specified any launch mode, the default launch mode is standard. In this case, the parent activity (B) is popped off the stack, and a new instance of that activity is created on top of the stack to receive the intent.
If you have specified launch mode as singleTop, the parent activity is brought to the top of the stack, and receives the intent through its onNewIntent() method. That is, the previous activity is preserved.
Refer http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp.
In your case, you want to preserve the variable, therefore you should use singleTop launchMode.
If you are extracting the data in onCreate method of Activity B then when you hit the back button in Activity C, the onCreate method of Activity B is not called again. To get more clarity on the life cycle of an Activity I really suggest you go through this developer.android.com/reference/android/app/Activity.html
Coming to your question when you start Activity B from Activity A, store the string that you sent from A to B in a global variable rather than a local one, and perform check like
public Class B extends Activity {
private String stringData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xml_layout);
if(getIntent().hasExtra(yourKeyName)) {
stringData = getIntent().getStringExtra(yourKeyName);
}
//Do other stuff
}
}
In your activity B, always check before getting the String -
if(getIntent().hasExtra(KEY_NAME))
String s = getIntent().getStringExtra(KEY_NAME);
Reason being that when you press the back button then Activity B is not launched with the same intent that you create in Activity A. Hence the extras are not present.
You should be string the string extra as a global variable and always use a hasExtra check.
I think it is different processes.
The first one, when you start activity B from activity A, you pass
the data from activity A to activity B. It is obvious, so the data
will be present.
The second one, then you start activity C from activity B, because of
activity B is not on the foreground, then your data will be erased.
You can persist your data on activity B by many ways.
One of them you can implement a static global variable.
Second way, you can save your variables on activity B by saving the
data onSaveInstanceState and retrieve from onRestoreInstanceState or
onCreate
Third way you can listen onActivityResult on activity B from activity
C. When you starting activity C, you must use startActivityOnResult
and passing the data that you want to pass back on activity B. After
you finish your activity C, you must return the data that originally
from activity B.
And so on...
But the better way to achieve that is the second way, use bundle onSaveInstanceState and retrieve the bundle data when onCreate.

How to properly save data across Activities. savedInstanceState always appears to be null

I have 2 activities A and B.
I call activity B from activity A using startActivity(B);
I do some operations in B and call back activity A using startActivity(A);
I have noticed that instance variables in class A are cleared (reinitialized). Is using onSaveInstanceState(Bundle) a correct way to save these variables before A gets stopped?
In my onCreate(Bundle savedInstanceState) method of class A, I try to retrieve the data previously saved by onSaveInstanceState(Bundle). However the variable savedInstanceState is always null and I can't retrieve any data.
If you want to return to your previous activity, you must call finish() on activity B, and then intercept it in activity A with onActivityResult().
Is that what you tried to accomplish?

Saving the instance of an Activity to reuse it

I have an Activity A with a button on it which when clicked navigates to Activity B. Now from this Activity B when the back button is pressed i get back to Activity A which is quite obvious. But once again when i click on the button on Activity A a new instance of Activity B is launched.
My query: Is there a way so that when i press the back button on Activity B it's instance is stored and so when i again click the button on Activity A the saved instance of B is launched instead of a new one.
Hoping for a solution..
if you don't want to pass data.. or just want to go back on Previous activity
finish() in onClick
http://developer.android.com/reference/android/app/Activity.html
In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created.
To think that it won't be easy to deal with it. The activity in the android can be store but you cannot call the android system to using that instance of your when you navigate to the activity B.
Read this link http://developer.android.com/reference/android/app/Activity.html
it give all information that you can do with it but not all as you want.
When you navigate from activity B to activity A the android itself has been store your activity B's instance temporarily in the memory and that instance will destroy completely anytime later. If you navigate it from A to B again, then the instance of Activity B will be reuse again if it still exist in the memory otherwise it recreate the activity again. In any case the activity B is reuse or recreate, the method onCreate is always call (all method in Lifecycle like onResume ...etc) then all data of your activity B is now new data that's not your old data before you navigate it to the activity A. This is what android system doing so that you cannot launch activity B without calling those method.
if you want the activity B look like the same as before navigate to activity A you should probably saving the necessary data in the method onPause() on the activity B then set those data back to the component of activity B when it call onCreate(). The method onSaveInstanceState(Bundle) should not be use in this case because the android os version which is before honeycomb (3.0) is not invoked that method.
This could be difficult to deal if you are working with online data or listview, webview ...etc and also save data by your own it could the risk to your application itself.
Anyway I hope this could be some help to you.

Navigating through multiple activities

I have 2 Activities A and B. Now these are my objectives.
When I'm in B and if I press the Home button, the state of the Activity should be saved. (No problem with this.)
When I start B from A after step 1 a new instance of B should be created (i.e) Previous state should be discarded.
But in Step 2 the state of B still prevails. How do I accomplish my objective?
I think one possible solution woudl be to pass some extra information inside the starting Intent, when you start Activity B from A (like a boolean value). And in the "onStart()" of B, you check if you can find this extra info in the intent (you get it with getIntent()). If it's not present, that means you do reload the activity's previous state. If it is, then you don't reload it.
refer this url
Android; How can I initialise state in one activity, then have another refresh that?
You don't even need to send a boolean like Scythe suggested. The Bundle savedInstanceState will be null in onCreate for Activity B if Activity A just started it, whereas it will be non-null if you are coming back from a saved state.

Categories

Resources