Activity calling other activities on Android sequence - android

How can I save and remember the activity that got launched by a specific activity? Say I have activity 1 which launches activity 2 using startActivityForResult().
Then I see the following sequence of calls:
onPause called from Activity1!
onSaveInstanceState called from Activity1
onActivityResult called from Activity1
Later when I hit the back button to go back to Activity1 from Activity2, I see the onRestart called:
onRestart called from Activity1
onResume called from Activity1
So my question now is how do I identify that the transition is from Activity2 -> Activity1 rather than (say) Activity3 -> Activity1?

One possible way is to use startActivityFprResult instead. When you finish any of your other Activities (in this case Activity2 or Activity3), you call setResult(RESULT_OK, intent) and provide an Intent. This will be delivered to Activity1 in onActivityResult, and you can just put some extra in the Intent to identify which Activity just finished.

Related

Save old activity data android

Two activities A,B.
When A starts data is defined. From A to B activity is called,
And A activity is finally destroy.
How can i stop destroying A activity ?
And get the same oncreate data when came back from B.
If you are using Intent for navigation from A to B. Don't call finish() method. Just start your B Activity.
for ex :
Intent intent = new Intent (A.this,B.class);
startActivity(intent);
//Here dont use finish() method as it destroys current Activity.
Hope it will help :)
If you go to ActivityA to ActivityB then ActivityA is not destroy by default. if you give finish() method than and than only ActivityA will destroy.Otherwise ActivityA is only stop but still into background state.So,do not add finish() method when you transfer from one activity to another activity.
What you can do for your situation
Intent intent = new Intent(ActivityA.this,ActivityB.class);
startActivity(intent);
And When you can back to ActivityA it will call onStop() method of ActivityA.
So, you can define your date in onStop() method again.

Where activity results are sent to, if new activity is started for result and then main activity closed?

I am starting new activity for result (let's name it Activity3), but then immediately finish the main activity (Activity2):
mActivity.startActivityForResult(mIntent, PICK_USER_PHONE);
finish();
in result, onActivityResult is not called. Does it mean that result from Activity3 is lost? Or can I get that in some other activity (for example in Activity1, which started Activity2)?
if you finish, yes, it is lost.
If you want Activity1 to receive it, you have to start Activity2 for result, then start Activity3 for result, then receive the result on Activity2 and then you can finish() Activity2 and pass the result back to Activity1

why onCreate() is called every time when its started activity is finished?

In my app, there is a activity A, which it's a main activity, also there are several fragments inside A. When you click the images in one of fragments, it will start a new Activity B. When you click back button, i will call finish() to finish the activity and return to Activity A. But when returning to the Activity A, onCreate() of A is called again. Why onCreate() is called each time? As i know, it should be just called once, and then onStart() should be called.
From segment to the Activity B is as below:
Intent i = new Intent(_scontext, ProductListing.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_scontext.startActivity(i);
getActivity().overridePendingTransition(R.anim.push_out_to_left,R.anim.push_out_to_right);
When click back button in the Activity B, the code snippet is as following:
Intent _gobck = new Intent(_ctx,ProductDisplay.class);
startActivity(_gobck);
finish();
overridePendingTransition(R.anim.push_out_to_left,
R.anim.push_out_to_right);
What's wrong with the code? Am i missing anything?
You are starting the activity again. Remove the following code and it will work.
Intent _gobck = new Intent(_ctx,ProductDisplay.class);
startActivity(_gobck);
Since you already got your answer by #Rajitha Siriwardena but i just want to clear some of the points here,
As i know, it should be just called once, and then onStart() should
be called.
Above sentence is not a true first of all .
There is possibility to for your ActivityA to go in OnCreate even if you finish your ActivityB. If your ActivityB stay in foreground for a long time ,of-course your ActivityA will be in background in that case , so ultimately your Activity in onStop (remember not in onPause) and android Activity life cycle doc says, after onStop if your app want reach your Activity then it will goes in onCreate
So finish() ActivityB would work but there is no guarantee to your ActivityA called onCreate when you do so .
if you remove finish() from your backPress Activity will not be created and you don't need to write Intent it will manage it's back stack it self.

How to open intent and add value android

I have trouble with returning a value to another intent in android.
This is my situation:
I have Activity A, which is opened when I start my application.
When I click on a button I start Activity B.
In activity B I fill in a EditText and return a string.
Then I click on a button in activity B to return to activity A.
I don't know what method I should use in activity A to use the value I get returned from activity B.
this kind of situation fits good for startActivityForResult and onActivityForResult. When you press on the button, instead of calling startActivity you call startActivityForResult in order to start ActivityB. In ActivityB when you click on the button, you have to fill up an Intent with the values you want to return to ActivityA and, in order, call setResult(result, Intent) and finish(). The onActivityResult of ActivityA will receive the intent with your data
Activity Docs
You should start activity B by calling startActivityForResult() instead of startActivity() and on Activity B, in your exit button click listener, setResult() results to pass to Activity A.
You can handle the result by overriding onActivityResult() method in Activity A
Check this : http://developer.android.com/reference/android/app/activity.html
Search for "Starting Activities and Getting Results"

Intent flags on Android

I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity A.

Categories

Resources