How to kill particular activity from task? - android

In my application I have 6 activities like 1->2->3->4->5->6. In 6th activity user can choose any activity(I am launching with intent). Means he can choose 1 or 2 or 3 or 4 or 5. Now when I press back button it is coming back to 6th activity again(which I do not want).
ex:
I have executed like this 1->2->3->4->5->6. Now I am in 6th activity. Here I have button, when I press it, it launched 4th activity. My code is like this on button click.
Intent intent = new Intent(MainScreen.this, UniversitiesScreen.class);
startActivity(intent);
So now I am in 4th activity. When I press back button it taking me to 6th activity(which I came from) instead 3rd activity(what I am expecting).
Can any one help me to handle this type of navigation.

Simple answer is edit your onBackPressed method in every activity...
For example, Write in 4th activity like this
#override
public void onBackPressed(){
Intent in = new Intent(this, ThirdActivity.class);
startActivty(in);
finish();
}
Please check the context that you are passing to intent. Every activity is different so pass the activity context from where you are calling another activity.
If my guess is true Mainscreen.this is first activity, and if you call fifth activity from fourth activity, give intent as
Intent in = new Intent(FourthActivity.this,FifthActivity.class);
startActivity(in);
finish();
And you are not saving any data in backstack and you are going back manually...

The best way is to use the NavUtils.navigateUpFromSameTask(this) function in your activity. For this you need to define the parent activity in the manifest for each activity so when you are in the 4th activity then the parent activity is the 3rd activity.Then when you click on back or navigate up you end up in the parent activity i.e. from 4th to the 3rd.
Take a look at this
http://developer.android.com/training/implementing-navigation/ancestral.html
Look at the manifest and then the Navigate up to parent task section
This will also help in returning to the same state of the 3rd activity from the 4th activity.

Related

Check if a previous activity exists

I have an activity. After checking some stuff, I want to go back to previous activity if a previous exists, and if not I want to start a SpecificActivity. How do i do it?
Edited:
Seems like no one is understanding what I meant, so let me rephrase. Lets say I am in Activity A. I don't know if A is the only activity in the stack. If there are other activities, then I want to finish A and pop the activity right below A in the stack into the foreground. If A is the only activity in the stack, I want to start some activity Z. How do i do it?
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can simply override the onBackpress()
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(this, Destination_Activity.class));
finish();
}
call the next activity like.
Intent intent = new Intent(this,Your_Next_Activity.class);
startActivity(intent);
then it will call your another activity and your current activity will be on background if you will use finish() after calling next activity it will finish your current activity so don't use finish() after calling your next activity in this scenario.
After that when you press back button it will automatically finish current activity.

Android Activity management

I have one question in mind for activity management. Suppose I have 4 activities say for example A1,A2,A3,A4. Now A1 have one button which start activity A2. A2 have 2 buttons which start either A3 or A4, A3 have 2 buttons which start activity A4 and A1. A4 have 3 buttons to sart activity A1,A2,A3 I do not use finish method in any of this activity. So now user click any of the activity any of the button than check the activity ,that is this already in back ground? If yes than this activity would not generate new instance and start activity which is already in background. otherwise it create new insistence.
You can get this behaviour by including the FLAG_ACTIVITY_REORDER_TO_FRONT in your Intent's flags and then just calling startActivity(intent) like you normally would.
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
You can search "android:lunchMode" by Google.
Then you will get the anwser.
Whenever the button is clicked in any activity, it creates the new instance of the activity irrespective of the fact the activity is already on the activity stack. Since new Intent is fired every time, it opens new activity.
When we press back button then only it goes to the already opened activity from the stack.

Starting Activities in Android

I'm a newbie in Android after Googling I found if you want to call another activity you have to use
Intent intent = new Intent(getBaseContext(), Activity_Name.class);
//if Any Extra
//intent.putExtra("", "");
intent.putExtra("EmpID", EmpID);
startActivity(intent); `
Now this is a standard approach but if you refer to the image. I'm lopping almost 20 times in the ListView (Activity1).
Now what I belive that this keeps adding again and again as my app crashes later on without any proper reason and not at any particular location.
I have disable the back button on the DataCollection Screens (Activity 2, 3,4)
Hence its a pure Waterfall approach.
Any suggestion. Should I add finish(); line in the last screen
Intent intent = new Intent(getBaseContext(), Activity_Name.class);
//if Any Extra
//intent.putExtra("", "");
intent.putExtra("EmpID", EmpID);
startActivity(intent);
finish(); `
So that it dies and goes to the previous and that dies and goes to the previous. And later landing on the ListView.
Would that be a good approach. Or is there anything where I can just call another activity like this but the system should forget all the previous activity information.
Or is there anything where I can just call another activity like this
but the system should forget all the previous activity information
Have a look at Intent Flags, e.g.FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_CLEAR_TOP.
If you dont want the user to go backwards, finish the current activity just after starting a new one.
after
startActivity(intent);
call
finish();
If you'd like to clear the activity stack, when going to a particiluar activity find it your manifest (like "Activity 1") and add the attribute android:clearTaskOnLaunch="true"
more info here : http://developer.android.com/guide/topics/manifest/activity-element.html#clear
You are probably better of using startActivityForResult rather startActivity. Then, when you call an activity you know that when it finishes it will return to the Activity which called it (it will come into onActivityResult). This way you can achieve:
Activty 1 -> Activity 2 -> Activity 1 -> Activity 3 -> Activity 1 etc
or
Activty 1 -> Activity 2 -> Activity 3 -> Activity 2 -> Activty 1 etc
In both cases, you don't can keep "passing through" phases of an activity invisible, they can just receive the "activity you called" trigger and pass on to the next activity.

saving a specific activity through intent

I have a main activity that has a start button that will intent to the next page(2nd activity) then from that 2nd activity there's a button also that will intent to the next page (3rd activity), my question is
1.how can I save the 3rd activity after 2nd activity intent to it?
2.If I exit the app and open it again, the start button on my main activity should intent me to the 3rd activity and not on the 2nd activity
Do you call finish() between the transitions? You should not. Android stores the Activity stack for you so when the user comes back into the app all three (1, 2, 3) activities will be on the stack.
Now, for jumping from 1st -> 3rd instead of going 1st -> 2nd -> 3rd you could store a flag that the 1st Activity will check. If the flag is present then call startActivity(Intent intent) for the 3rd activity directly.
Another way is to have 2nd Activity check that flag in onCreate() and if its present immediately start the 3rd Activity. Make sure to call finish() in onCreate() since this will avoid the rest of the lifecycle methods (onStart() onResume() etc.) to execute.
Hope this helps.

How to implement Back button functionality

I have 3 different activity namely, category,sub_category and list of items. In sub_category i created back button using intent Activity. Back button in list_of items should be focus to sub_category. i dont know how to do this.. pls guide me..
from category activity
call
Intent it=new Intent(category.this,sub_category.class)
startActivityForResult(it);
similarly
Intent it=new Intent(sub_category.this,list_item.class)
startActivityForResult(it);
and in onClickListener of back button simply finish your activity like
list_item.this.finish();
for going back to previous screen u can call finish() in your current activity. It will remove the current activity from activity Stack and take you to the previous one.

Categories

Resources