Android-finish function in activity didn't work - android

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

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

How to get the current activity name and the previous activity in android?

I have two activities in my Android app code: A and B. I have a function f() inside A which I want to call from A's onResume() function only when the previous running activity was B, but not in any other cases. How do I know which activity it came from, whether it came from activity B or some other activity?
Activity A
Intent intent = new Intent(this,B.class);
intent.putExtra("Activity_Name","A");
startActivity(intent);
If summarize in one line Activity A :
startActivity(new Intent(this,B.class).putExtra("Activity_Name","A"));
Activity B
Log.e("From_Activity",getIntent().getStringExtra("Activity_Name"));

Android Backbutton Pressed Return to Two Activities

This may sounds simple, but I am so bewildered by it. I have searched but not found any solution.
My question is: How to return to two activities when back button pressed?
Like this: let me say that I have activities A, B and C (A -> B -> C). What I want to achieve is when I am on activity C and press the back button, It should return me to activity A. When I am on B and press back, it should return me to A too.
It may be implemented into a project with many activities, so I assume that I don't need to set the class name of where to return, It should be recorded automatically by the android. How to achieve this?
Thank you
A possible solution is calling startActivityForResult() from Activity B, so that on the callback of the created Activity C, the previous Activity B gets finished as well. The callback function is called onActivityResult(), and in that, you want to call finish().
Example:
ActivityB:
Intent i = new Intent(this, ActivityC.class);
startActivityForResult(i, 0);
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
this.finish();
}
ActivityC:
//do stuff
This way, when you press Back (or call finish()) in ActivityC, it will call back on ActivityB's onActivityResult() function, and it will end them both.
You can override the onBackPressed method and sent an intent to the activity you want.
#Override
public void onBackPressed()
{
// code here to send intent to the activity A
}
One thing, I'm not sure if this goes well with the activity stack but you can alway try.
you can finish activity B , when you are starting intent for activity C, then activity stack will have activity A, and when you press back on activity C, activity A will be displayed.
just override onBackPressed in Activity C, and finish() it.
You can call the Activity that you want to go back to with a special flag (FLAG_ACTIVITY_CLEAR_TOP) from your onBackButtonPressed which will skip/remove the other activities in between. This way you can go back from C to A.
See: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
I think this is better than messing around with the finish() or starting activity for result when there is no result to return.
A --> B --> C
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will launch a new intent for Activity A and clears all the other activities from Stack.

TabHost : Start the activity without destory other activities

I am new to android, I am using the TabActivity. From the TabActivity I am starting the activity from intent. Order of Activity A - B - C - D then from the activity D, How can I create the same new Activity A (destroy the previous A). If i use the FLAG_ACTIVITY_REORDER_TO_FRONT its does not create the activity, instead open the last Activity A, If I use the Clear_top then it destroy the B and C Activity.
Please help to achieve this.
When you are calling Activity B from within activity A, call finish() after creating the new activity B using Intent. This will end the Activity A there. Then again from witin Activity D you can create a new Activity A using intent. Hope this helps.
You might want to consider destroying the previous activity before calling the next activity
so when you are going to call the activity B from A you might want to destroy the activity A using the keyword finish()
and likewise when you move from B to C and C to D and in the D activity destroy the activity of C and call the new activity A that way the A activity will get restarted.
finish(); //finish the current class
Intent intent = new Intent();
intent.setClass(getApplicationcontext(), nextclass.class); //specify the next class
startActivity(intent); //start the next class.
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
android:launchMode might be the answer you are looking for. From documentation:
Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent.
Means the existing the activity (if exist) will remain in the current stack untouched and new instance will be created at the top of the current stack. So when user back buttons, the user will see your activity A at the bottom of the stack as well.

activity auto restart when i try to close android application

i have three activities (A, B, C). This is my activity circle A => B, B => C, C => A
when start activity C from activity B complete i don't call finish() function on B activity. when start A activity from C activity complete i all finish() function on C Activity. In A activity i implement back button to close application by use this code:
android.os.Process.killProcess(android.os.Process.myPid());
i want to close my app but the application restart B activity
android.os.Process.killProcess(android.os.Process.myPid());
Wont kill all the activity. you must use CLEAR_TOP flags in your intent. or you must use this
method to finish ur B activity from C activity if needed. Another thing is use finish()
Inside the onrestart overide method in your B activity.
when you start A from C just add FLAG_ACTIVITY_CLEAR_TOP to your intent
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Categories

Resources