how to really exit an activity which contain fragment? - android

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" ... />

Related

Android-finish function in activity didn't work

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();

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).

finishactivity not finishing the current activity

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();

Android recreate Fragment onBackPressed

I have two activities and one fragment in my app. The first activity, MainActivity, is used to hold the fragment. It contains an ActionBarSherlock ActionBar and the fragment, WFrag. WFrag opens up the second activity, SettingsActivity, when you press a button. In SettingsActivity, you can choose settings that affect the behavior of WFrag. Naturally, I want the onBackPressed() method to go back to WFrag and update it with the user's new settings. My question is: how do you make it so that when you go back (via the back button) from SettingsActivity, you recreate (call onCreate() again) WFrag?
override onBackPressed
or
add the update in onStart/onResume
First you have to override onBackPressed() of SettingActivity.
Second when you start second activity i.e SettingActivity from MainActivity you have to clear Stack of previous activity in this way
Intent intent = new Intent(this, SettingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Same goes for SettingActivity in onBackPressed() method you have to run your MainActivity in this way
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Or if you are redirecting to MainActivity from button click you can use above code for same.

Navigate to menu on back button press

I'm navigating from:
Main activity to Activity 2
Activity 2 to Activity 3
Activity 3 to Activity 4
through Intent.
I've also created the menu so that user can directly navigate from Activity 4 to Main activity. But after navigating from Activity 4 to Main activity by using menu, when I press back, it takes me to Activity 3 rather than exiting the application.
I tried:
#Override
public void onBackPressed() {
super.onBackPressed();
MainActivity.this.finish();
}
But no gain. Any suggestions?
Just try this code when you navigate from 4th Activity to Main Activity.
Intent inMain=new Intent(Activity4.this, MainActivity.class);
inMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(inMain);
Note:
This code in your case, clears the previous activities and launches the main activity with no activity in the backstack.
You can set flag Intent.FLAG_ACTIVITY_CLEAR_TOP to finish all the intermediate activities and your called activity will come to top of the activity stack.
use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP with your intent. For more detail -
FLAG_ACTIVITY_CLEAR_TOP
call startActivity Method using clear top flag
startActivity(new Intent(this, UI.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
You need to consider your "back stack" more closely.
What is happening exactly is, your back stack gets populated as follows:
Main activity -> Activity 2 -> Activity 3 -> Activity 4
Then from Activity 4 you launch your Main Activity. So your stack becomes:
Main activity -> Activity 2 -> Activity 3 -> Main Activity
Hence, when you press back, you land up in Activity 3.
Solution:
Either call finish() on every Activity when you navigate away from them. Or use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear all intermediate Activities.
More about Intent.FLAG_ACTIVITY_CLEAR_TOP.
When you go back to the MainActivity you need to use the Intent.FLAG_ACTIVITY_CLEAR_TOP on your Intent. This is an example of a goHome method that you can used in your Activity:
public void goHome ()
{
Intent homeIntent = new Intent();
homeIntent.setClass(this, MainActivity.class);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
In activity 2 and 3, after calling startActivity(intent), call this.finish(). This will also be one solution.
Finish previous activity when you are go to the next activity means write finish(); after startactivity(intent); and write below code for start first activity from fourth activity's button click event.
Intent in1=new Intent(Act4.this, Act1.class);
startActivity(in1);
finish();

Categories

Resources