finishactivity not finishing the current activity - android

I have activities called ACITIVTYA,ACITIVTYB,ACITIVTYC.
ACITIVTYB and ACITIVTYC extends ACTIVITYA
In ACITIVTYA which is the parent acitivty,i want to reload the ACTIVITYB. i used the following code
finishActivity(1000);
Intent intent = new Intent(ACITIVTYB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent,1000);
But the finishActivity(1000); doesnot work, when the same ACTIVITYB is in the screen.
It works from different activity ie when ACTIVITYC is in the screen i can reload the ACTIVITYB. But When ACTIVITYB is in the screen i cannot reload the same ACTIVITYB.

You can use:
getActivity().setResult(350, null);
getActivity().finish();

if you want to finish your current activty yout must have to remove that from activity stack befire going to next screen fir that you have to use
youractivity.this.finish();

Related

how to really exit an activity which contain fragment?

I have3 Activities.
The first one A contains a Fragment A1 which contains a Spinner.
When I click the Spinner i ll go to Activity B, which contains a Button.
When I click the Button will go to Activity C.
I ovverrode the Activity C's OnBackPressed
(
Super OnBackPressed()
Intent i = new intent (this,ActivityB.class)
Finish();
StartActivity i
}
I overrode the Activity B's OnBackPressed
(
Super OnBackPressed()
Intent i = new intent (this,ActivityA.class)
Finish();
StartActivity i
}
I overrode the Activity A's OnBackPressed
(
Super OnBackPressed()
Finish();
}
My problem is that each time I click on Android's return Button when I am in Activity A, the application doesn't exit and goes to my old Spinner choice.
I have to click several times to really quit.
What should I do to leave the application by simply clicking on the Android return Button when I am in the Fragment A1?
You can use getActivity().finish(); from Fragment
You can try to finish the current activity before moving to other one.
For ex: When in Activity A, you click on spinner, inside onclick you start activity B and then call finish(); This will start Activity B and finish Activity A.
This way you will end up with one activity throughout. Once u back press, you will directly come out of the application.
In Activity A, you can do this to make sure you clear your backstack, so your app doesn't go the old spinner choice.
In your onCLickListener
Context context = view.getContext();
Intent intent = new Intent(context, ActivityB.class);
startActivity(intent);
finish(); // call this to finish the current activity
or you can also, do that in the manifest
<activity android:name=".ActivityA" android:noHistory="true" ... />

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.

Go back to another or old Activity from a fragment in an activity

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

Android - How do I switch between 1st and 3rd activities?

I have a 1st and a 3rd activity. I wish to push a button on the 3rd activity and it finishes the 3rd and 2nd activity switching all the way back to the 1st activity(it's the MainActivity might I add). In other words, is there a way to finish automaticly the call stack of activities to a specific activity?
You can use method startActivityForResult() to start your activity.
You need to call method setResult() when you finish the activity.
And override method onActivityResult() to do what you want to do.
Actually, seems 2 methods can solve this.
Assuming Activities: A -> B -> C, and all alive in the same stack.
1) Use android:launchMode="singleTask" for Activity A. When C calls A, A starts and both B & C finish.
2) When starting A, adding flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
if you don't have to pass any data to the main activity, i guess this is what you are looking for: how back to the main activity in android?
execute this code at the button click in your 3rd activity:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this code clears your activity stack and reopens your main activity.

How to go back to an already launched activity in android

I want to go back to an already launched activity without recreating again.
#Override
public void onBackPressed() {
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
this.startActivity(i);
}
Instead of doing is there any other way to go back to another activity which is already created, which I get back with " Back button "
Add android:launchMode="singleTop" in the FirstActivity declaration in your AndroidManifest.xml like so
<activity
android:name=".FirstActivity"
android:launchMode="singleTop"/>
You could also try the following if SecondActivity was started from FirstActivity
#Override
public void onBackPressed() {
this.finish();
}
Whenever you start a new activity with an intent you can specify an intent flag like this:
// this flag will cause the launched activity to be brought to the front
// of its task's history stack if it is already running.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
or
// If set and this intent is being used to launch a new activity from an
// existing one, the current activity will not be counted as the top activity
// for deciding whether the new intent should be delivered to the top instead
// of starting a new one.
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
When starting SecondActivity do not finish the first activity so when you go back , you will be taken to the first activity.
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
On your second Activity, close the Activity calling finish() instead of creating a Intent for the other screen.
By calling finish() the Application will go back in the Activity stack and show the previous Activity (if its not finished()).
Don't call finish() on 1st Activity, when you start 2nd Activity. When back is pressed on 2nd Activity, it finishes itself and goes back to 1st Activity by default. onCreate() is not called for 1st Activity in this case. No need to override onBackPressed() in 2nd Activity.
However, Android can destroy 1st Activity when you are using 2nd Activity, to reclaim resources, or, if you have checked Don't keep activities in Developer options. In such case you can use onSaveInstanceState() to save data and onRestoreInstanceState() to use it again in 1st Activity. You can also get the saved data in onCreate(). Here is how it works. Also, onSaveInstanceState() is called only if android decides to kill the Activity and not when user kills the Activity(e.g. using back button).

Categories

Resources