I have an activity lets call it activity A which on creation downloads some data from a database. The data is stored in a custom ArrayList called myData.
When a user clicks on a button another activity called B is launched. Activity A passes some data to Activity B which I can do by making my class implement Parcelable.
My understanding when Activity B is lanuched that Activity A will be in either the onPause or onStop state. My question is will my arraylist myData still exist when Activity B is closed and the focus goes back to Activity A? If not how can I store this data?
As soon as you launch a new activity, the previous activity goes in onStop state.
The definition of onStop:
Called when the activity is no longer visible to the user, because
another activity has been resumed and is covering this one. This may
happen either because a new activity is being started, an existing one
is being brought in front of this one, or this one is being destroyed.
See this chart
Now when you go back to ActivityA, the array data will still be present. So when switching of activities takes place this data is not destroyed and recreated once the activityA is again started.
Yes, you can finish you ActivityB by using finish() method. And your data of arrayList will exist when you will be back to ActivityA. And i will suggest to declare arrayList as instance variable of ActivityA and fetch data on onCreate() method. so data will exist until activity destroy, so data will not fetch again when activity come to focus .
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.
My app has 2 activities which have no way to reference eachother; what I would like is when Activity B is created, an event is triggered in Activity A (I'm trying to "finish()" Activity A)
Is there some kind of event handler that I can create in Activity A to listen for such a thing?
Only one activity at a time can be active which makes it impossible for one activity to send a message directly to another in real time.
From your description, I think you need to use one Activity that hosts multiple fragments. The fragments can "chat" to each other as much as you like and both can be active at the same time.
I have a main activity and a sub activity.
The main activity starts the sub activity using startActivity, and passes an object in the intent.
The sub activity reads the object out of the intent in its onCreate action.
The sub activity updates the object, then returns to the main activity using startActivity, again passing the updated object back.
However, the main activities onCreate function is not called, so the code it contains to read the passed object does not run.
Further investigation indicated that the main activity onPause event is firing, i.e. it is only paused when the sub activity runs, so when the sub activity starts the main activity again, it just onResumes.
Does anyone know if there would be any disadvantages if I moved my data restore/store activities to the onResume and onPause events?
I'm not using the onCreate savedInstanceState, should I be?
How else do you pass a set of data items between Activities without using a database or those preferences? Should I be using a database? I have about 20 fairly individual data items.
Any help would be much appreciated,
Frink
I would check out the startActivityForResult() method rather than just startActivity()
That should give you a means to pass things back to the calling activity.
From http://developer.android.com/reference/android/app/Activity.html:
Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.
Have a look at the Activity life cycle here.
Also, consider starting your sub-activity using StartActivityForResult.
It is more likely calling onResume() or onStart() depending on the child activity and how much control it takes.
Look at this page about 3/4's down at the graph.
http://developer.android.com/guide/topics/fundamentals.html
The parent never gets destroyed so it wouldn't have to recreate it. It should just get paused. You can override onResume() and onStart() just as you would onCreate().
Edit: Is there anything given to the user in the subactivity? It sounds like you don't actually need an activity you just need a java class that you can call methods on.
Why don't you call startActivityForResult from the main activity and get the data back in onActivityResult ? This is the normal way to do it.