activity auto restart when i try to close android application - android

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

Related

How can I manage activities like fragments stack

I have many activities.
A is list activity.
B is form activity. And generated dynamically. I open this activity two time in a row.
C is result activity.
A -> B -> B like simple push new activity.
if result is success I want to clear all forms when I push C.
A -> B -> B -> C ==> A -> C.
if result is failed when Im in C activity, it can be return different activities like above.
A -> B or A -> B -> B
I use cleartop when I push C but it clear all activities how can I save A activity's state.
How can I manage activities like fragments.
* When I back from second B, first B should open *
You can achieve it by following below steps:
Set android:launchMode="singleTask" of ActivityA in AndroidManifest.xml file.
Set onNewIntent method in ActivityA like below:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle mBundle = intent.getExtras();
if(mBundle!=null){
String launchActivity = mBundle.getString("activityName");
switch (launchActivity){
case "ActivityD": // This is Activity Name Here it is ActivityD.class
startActivity(new Intent(ActivityA.this, ActivityD.class));
break;
}
}
}
Now Start ActivityA from ActivityC like below.
startActivity(new Intent(ActivityC.this, ActivityA.class).putExtra("activityName", ActivityD.class.getSimpleName()));
It will call onNewIntent method of ActivityA and will match the argument and launch ActivityD from ActivityA. So your ActivityA will remain in stack and ActivityD will be added in stack on Top.
Now to achieve A -> B from A -> D you can call finish() method in ActivityD when you start ActivityB from ActivityD.
Regarding ActivityLaunchMode please refer this link
Hope it will work for you!
On Activity A you cant goto Activity B using this
startActivity(new Intent(Activity_A.this, Activity_B.classs));
From B to C
startActivity(new Intent(Activity_B.this, Activity_C.classs));
finish();
From C to D
startActivity(new Intent(Activity_C.this, Activity_D.classs));
finish();
From D to A
finish();
It will close Activity_D and Resume() Activity_A
When you startActivity from Activity B to Activity C or C to D you need to call finish(); expect for Activity A.
Like
A -> D.
Start Activity(new Intent(A.this,D.class));
Now, when you click Activity A --> B then B--> C and then C--> then you should do like
A -> B.
Start Activity(new Intent(A.this,B.class));
B --> C
Intent intent =new Intent(B.this,C.class);
startActivity(intent);
finish();
C --> D
Intent intent =new Intent(C.this,D.class);
startActivity(intent);
finish();
Now, When you press back you will be return to activity A. You should remove all clearActivityTop(); from code.
You can achieve that by simply using fragments more often
Activity:: When an activity is placed to the backstack of activities the user can navigate back to the previous activity by just
pressing the back button.
Activity can exist independently.
Fragment:: When an fragment is placed to the activity we have to request the instance to be saved by calling addToBackstack() during
the fragment transaction.
Fragment has to live inside the activity
source
By using fragment you can easily remove any page you want by using tags, however if you still want to use activity
if you use startActivityForResult() instead of startActivity(), then
depending on the return value from your activity, you can then
immediately finish() the predecessor activity to simulate the
behaviour you want. By using this method in all your activities, you
can have this behaviour cascading the activity stack to allow you to
go from activity D to activity A.

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

How to manage multiple activity interactions in Android?

In my activity, i'm calling activity B from activity A (in back stack A->B), than again activity C from B (in back stack A-> B-> C) for some result to reflect on activity A, than how to jump to activity A on button click with carrying some result from activity C to reflect back in activity A(while clearing back stack of activity B,C)
Option 1: You can chain calls of startActivityForResult() and "unwind" the chain in the onActivityResult() callbacks.
Activity A calls startActivityForResult() to start Activity B
Activity B calls startActivityForResult() to start Activity C
Call setResult(RESULT_OK) and then finish() in Activity C
Result of Activity C is received in onActivityResult() of Activity B. Process the result (if needed).
Call setResult(RESULT_OK) and then finish() in Activity B
Result of Activity B is received in onActivityResult() of Activity A. Process the result.
Option 2: Start Activity A again, but add flags to the intent.
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// add more data to Intent
startActivity(intent);
finish();
This intent will be received in the onNewIntent() method of Activity A.
EDIT
Option 3: Use LocalBroadcastManager to send broadcasts locally (within your process only). This requires registering a BroadcastReceiver dynamically in Activity A, then sending the broadcast from Activity C. You will still need to use one of the two techniques above to get back to Activity A and clear the stack; this just changes how the data is transmitted back to Activity A.

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.

Categories

Resources