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.
Related
What I am trying to do is, there is no chance the two activities are running at the same time. So I am using for this method in my adaptor class.
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
((Activity)context).finish();
But when I click the back button, it doesn't go to the back activity.
What am I doing wrong?
Basically you should remove finish() method from your code so that it will not destroy that activity and keep it in stack.
Once you call the finish() you can not go back to previous activity. This question explains in details, what happens when you call finish() method.
Remove ((Activity)context).finish(); in your code,
because
here you are finishing your activity which means when you press back you don't have any activity on your stack to go back to.
Finish() method will destroy the current activity. You can use this method in cases when you dont want this activity to load again and again when the user presses back button. Basically it clears the activity from the current stack.
So,no need to use finish() here
Just remove this line:
((Activity)context).finish();
Called the Finish method of activity at button click.
finish() just lets the system know that the programmer wants the current Activity to be finished.
((Activity)context).finish();
That is because you are finishing your activity which means when you press back you don't have any activity on your stack to go back to.
so just remove finish so that i will push WinnerDetailActivity on top of your current activity and on back of WinnerDetailActivity it will open your current activity.
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
And also read about FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at
the top of the history stack.
//Here intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); will alway remain in intent stack unless you finish it and once this activity is resume.. then it will act as the stack has only on Activity running...
//So just remove ((Activity)context).finish();
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
((Activity)context).finish();
There is two way to resolve this
1.Just remove ((Activity)context).finish(); because Finish() method will destroy the current activity.
2.Just Add below method in your class
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(CurrentActivity.this,DestinationActivity.class));
}
DestinationActivity means Activity where you want to move.
OnBack Pressed Method is used to go back to previous activity,
super.onBackPressed() is used to navigate to previous activity without super.onBackpress it is not possible to traverse to previous activity.
#Override
public void onBackPressed()
{
super.onBackPressed();
}
This can be done in two ways:
Method 1:
Whenever you're stating a New Activity from an Activity, Make sure that you don't call finish(). Calling finish() will destroy the Previous Activity in the Stack.
Next Method is by Overriding onBackPressed(), by doing so you can navigate to the desired Activity.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent( thisActivity.this, yourFirstActivity.class);
startActivity(intent);
finish();
}
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).
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.
I have an application that starts activity A. The user can then start service S from activity A. When a certain event happens, service S starts activity B. Activity B only has one button, and when pushed, should return to activity A. Everything works fine, except for when activity A was closed out using the back key. When the back key is pressed, instead of onPause, onDestroy is called. So when activity B is dismissed, I get sent back to the homepage instead of Activity A. So my question is, how can I make sure that Activity B reopens activity A if activity A is onPause, and also reopens the activity A when it's onDestroy has been called. I'm thinking to use intent filters, but I can't figure out to get the right combination. I do not want multiple instances of Activity A. Thanks for the help.
on activity A:
public void onBackPressed() {
moveTaskToBack(true);
}
or on Activity B, force it to start activity A,
public void onBackPressed() {
Intent i = new Intent(B.this, A.class);
startactivity(i);
}
but since you implement the first one,
you dont need this last code.
I Hope it helps
Since you don't need to save A state,
you can override onBackPressed() method in B to start A again.
And if you don't want multiple instances of A, use FLAG_ACTIVITY_CLEAR_TOP when you start B.
Hope this helps
I am having application in which i am using menu , on tap on menu item i am redirecting to specified activity.
I want to make sure that when i am redirected to another menu item my current all activity should be finished to reduce the stackflow of the activity and better performance.
So when i tap on back from my tapped activity from the selected menu activity i should be redirected to another activity and finish current activity.
So i am wondering is there any way by which i can finish another activity from my current activity. Or should i override the OnKeyDown Method..
Any help on this
Thanks in advance
You can do:
Intent intent = new Intent(....) ; // intent to launch the new activity
// fire intent
finish(); // finish current activity
Intent i=new Intent (currentclass.this , nextclass.class);
startActivity(i);
finish();
Use finish() as shown above ,and here we are redirectin to nextclass.class so all activity of currentclass should be finished to reduce the stack over flow ..
and mind it finish() calls onDestroy() .and onDestroy() is executed...and destroy current activity(in my case currentclass)..
also mind it. onDestroy() isn't a destructor (as in c++) .It doesn't actually destroy the object of current activity... That's it....