TabHost : Start the activity without destory other activities - android

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.

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 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 activity to load the old activity instead of creating new instance of that old activity

In My application there are three activities A, B and C and i have kept two buttons in every activity (Previous and Next button) so that user can move from one activity to another. Suppose User is in activity B and user wants to navigate to Activity A.
Here my problem is.. I need to load the old activity instead of creating new instance of Activity A.
Just call finish on the current activity and the previous one will be shown automatically.
Try to call your intent with the flag "BROUGHT TO FRONT"
Intent i = new Intent(...);
i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(i);

calling an activity from stack instead of launching new instance

how can i call an activity from stack instead of launching new instance ?
here is a scenario :
calling activity A with parameters in order to retrieve data
navigate from A to B
navigate from B to C
i want launch A again but not with a new instance , i want get it from the stack so no need to pass parameters and wait to retrieve data.
If I get your point correctly you can simple exit your activity B and C with finish();.
So if you ActivityC finishes and also ActivityB the ActivityA should come to the front, which should be that what you want.
Try to use FLAG_ACTIVITY_REORDER_TO_FRONT. For example:
Intent intent = new Intent(BActivity.this, AActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
From javadocs:
public static final int FLAG_ACTIVITY_REORDER_TO_FRONT
Added in API
level 3
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.
For example, consider a task consisting of four activities: A, B, C,
D. If D calls startActivity() with an Intent that resolves to the
component of activity B, then B will be brought to the front of the
history stack, with this resulting order: A, C, D, B. This flag will
be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.

Activities behavior

I have four activities A,B,C,D. I am passing an text from activity A to Activity B using bundle and Activity B is displaying it nicely. Then i move from Activity B to Activity C and then Activity D. After that i called Activity B from the Activity D with the help of intent and i seeing no text are there in Activity B.
Please suggest me the way to keep the text there, with some code example.
You would need to use either FLAG_ACTIVITY_CLEAR_TOP or FLAG_ACTIVITY_REORDER_TO_FRONT.
Without one of these you are creating a new instance of your activity instead of revealing the old one. You can set activity flags on the intent you start from activity D:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
before you call startActivity.

Categories

Resources