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.
Related
What I am trying to do is, there is no chance the two activities are running at the same time. So I am using for this method in my adaptor class.
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
((Activity)context).finish();
But when I click the back button, it doesn't go to the back activity.
What am I doing wrong?
Basically you should remove finish() method from your code so that it will not destroy that activity and keep it in stack.
Once you call the finish() you can not go back to previous activity. This question explains in details, what happens when you call finish() method.
Remove ((Activity)context).finish(); in your code,
because
here you are finishing your activity which means when you press back you don't have any activity on your stack to go back to.
Finish() method will destroy the current activity. You can use this method in cases when you dont want this activity to load again and again when the user presses back button. Basically it clears the activity from the current stack.
So,no need to use finish() here
Just remove this line:
((Activity)context).finish();
Called the Finish method of activity at button click.
finish() just lets the system know that the programmer wants the current Activity to be finished.
((Activity)context).finish();
That is because you are finishing your activity which means when you press back you don't have any activity on your stack to go back to.
so just remove finish so that i will push WinnerDetailActivity on top of your current activity and on back of WinnerDetailActivity it will open your current activity.
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
And also read about FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at
the top of the history stack.
//Here intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); will alway remain in intent stack unless you finish it and once this activity is resume.. then it will act as the stack has only on Activity running...
//So just remove ((Activity)context).finish();
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
((Activity)context).finish();
There is two way to resolve this
1.Just remove ((Activity)context).finish(); because Finish() method will destroy the current activity.
2.Just Add below method in your class
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(CurrentActivity.this,DestinationActivity.class));
}
DestinationActivity means Activity where you want to move.
OnBack Pressed Method is used to go back to previous activity,
super.onBackPressed() is used to navigate to previous activity without super.onBackpress it is not possible to traverse to previous activity.
#Override
public void onBackPressed()
{
super.onBackPressed();
}
This can be done in two ways:
Method 1:
Whenever you're stating a New Activity from an Activity, Make sure that you don't call finish(). Calling finish() will destroy the Previous Activity in the Stack.
Next Method is by Overriding onBackPressed(), by doing so you can navigate to the desired Activity.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent( thisActivity.this, yourFirstActivity.class);
startActivity(intent);
finish();
}
I extend activity A, B, C from MainActivity.
A start in first and A start B and B start C.
when I use finish() in onclick special Button in activity C, Activity A is start, but I want activity B start.
What I do for this case?
Start all activity like below:
Intent i = new Intent(this,NewActivity.class);
startActivity(i);
don't add finish() method, then inside button click just call finish() method. It will bring you back to previous activity.
You can't reliably expect Activity B to be on the stack when Activity C finishes since Android can destroy a background Activity if it needs memory. If your use case suggests that Activity B should always become visible when Activity C is closed, you should start it explicitly, as described in Navigating Up to Parent Activity. In your case, I think it's appropriate to use NavUtils.navigateUpFromSameTask(this) in onBackPressed().
AActivity.java
Intent i = new Intent(this,BActivity.class);
startActivity(i);
BActivity.java
Intent i = new Intent(this,CActivity.class);
startActivity(i);
CActivity.java
Intent i = new Intent(this,BActivity.class);
startActivity(i);
finish();
I have looked for this but, can't seem to find the answer that I need.
So, my scenario is, I have activityA that starts activityB using Intent. Then activityB sets up some values in a bundle and starts a fragment using fragment manager. Now, I want to go back to activityA when a button is click. I also want to pass some variables from the Fragment to activityA. How do I do this?
Currently, I just start just the activityA again by using intent with some bundle of data passed in from the fragment. This is not good, because I am restarting activityA again. I just want to go back like using OnResume() or something similar, so that my old data is still there in activityA again.
Thanks
You can either call finish like you would do from an Activity but like so:
getActivity().finish();
Or you can start the Activity and recall the previous one from the stack:
Intent i = new Intent(getActivity(), activityA.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
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.
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.