Before I say anything else, I am not asking how to make this work for my program, I want to figure it out myself. I have already seen one billion snippets of codes and answers. But I think I am not understanding the concept behind it and would like to verify if my understanding is correct.
Creating a back button and making it work:
1) Find and edit the xml for the action bar so show a back button.
2) Use the buttons id to create OnClickListener.
3) In OnClick, use intent to previous activity.
Is it not this simple? What are the proper steps conceptually for make this work?
That might work, but it wouldn't be the norm.
Depending on how you launch an Activity, simply calling finish() on an Activity will take you back to the previous one.
easier to
1) use AppCompatActivity
2) enable homeAsUp (back button)
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true)
Android activities are stored in the activity stack.
So simply by calling the finish() method if you started the activity with startActivity()
this.finish()
or if you opened the new activity from another activity with startActivityForResult()
call the finishActivity() method from your code on the click of the button
this.finishActivity()
And it'll take you back to the previous activity.
Well..... I don't know this answer would be helpful but, you said
3) In OnClick, use intent to previous activity.
However, how about thinking this situation?
If there is fragment and you press 'back button' in there, you should go before fragment not before activity. :)
You said you want to figure it out yourself so, I won't say more(about codes) as you wanted. Good Luck!
Related
First of all I read this article but is kind of complicated for me . All I need is to go to an activity I want (navigate if exist in back stack or start it with Intent if it's not ) in onBackPressed() override . should I manage back stack
or something else ? if yes how ? and if no what is a simple way for that ?
P.S : I dont use ActionBar
you should override the second activity's onBackPressed() method and add android:launchMode="singleInstance" to your first activity in AndroidManifest.xml this will launch the first activity from backstack or create new if it doesn't exist in backstack.
Use startActivityForResult() wherever needed. Do not create new instances of the same activity. That is, if u want to go back to the previous activity, just call finish() from this activity.
Understand different launchmodes
As mentioned in the link above, singleInstance launchMode might be tricky and might cause issues.
SOLVED: as mentioned in this answer and according to comments , if we want to just bring an (existing )Activity to front , setting Intent flag to :
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
wont create new instance of Activity we want and just bring it front . this would be useful if we have multiple activities on top of the one .
I am beginner to android developing and I am dealing with a probably simply solvable problem.
I have a Main activity which is a menu and the user can click on of the items on menu. Then a new Activity is started using startActivity(new Intent("com.example.test.FIRSTITEM")); this activity starts a layout with few edittexts. I write something into them. Then to go back to the MainActivity (where the menu is) one have to press back button.
And now I want the behaviour that if I return to the Activity with edittexts, I want them to be in the same state - meaning the content wouldn't be cleared.
I've searched a lot and tried all options of android:launchMode="" (singleTop, singleInstance, singleTask).
I've also tried to use flags when starting new intent from MainActivity:
startActivity(new Intent("com.example.test.FIRSTITEM").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP))
I've tried using onSavedInstanceState(Bundle bundle) but this one is never called when pressing back button (probably because the activity isn't really finished)
I am sure the answer is simple but I can't seem to find it. I must be missing some logic of how activities work.
Thank you for your help in advance.
I think this this could help you out.You can use
onsaveinstancestate() and onrestoreinstancestate().
I have 4 activities. A,B,C and D.
From each activity user can start any activity.
i.e. Activity A has 3 buttons to start B,C and D. In same way all other activities also has 3 buttons.
Now my question is If user go in this sequence A->B->C->B.Now If user press back button then this sequence happens. B->C->->B->A. I want to change in this way B->->C->A. how can i do this?
I don't clearly understand your question, but maybe try use:
android:launchMode="singleInstance"
You want to keep the same instance all the time, not create a new one, yes?
finish activity B using finish() when you moved in the forward direction that means A>B(finish())>C>B
Like #Krishnakant said you need to add finish(); everytime, As far as I'm aware the back button is called as the Achille's heel for android programming as coding or adding handlers to it is not easy.
use intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); before staring any activity.
I have been searching for an answer but I couldn't find a proper one. The question is that I have a dialog themed activity on top of a normal activity. I would like to force the users to either read and click "OK" to the themed activity which will then transfer them to another dialog themed activity for some further questions or cannot enter the application. So, I would like to exit the application, on back press, and not just finish the themed activity that will reveal the content of my app. How is that possible?
If you start your dialog activity with startActivityForResult() you can send back the result RESULT_CANCELED from the dialog, and upon receiving this (in your main activity) you call finish().
finish() will do the perfect job for you ;)
but make Dialog not cancelable..
Edit (after problem description clarification):
As others said StartActivityForResult could work, with additional trick.
Because of the way you design your App (DialogActivity1->DialogActivity2), it might help to add following line in AndroidManifest file, for all your special dialog-look activities:
android:noHistory="true" or to set flag for intent Intent.FLAG_ACTIVITY_NO_HISTORY before u start DialogActivityN.
Both lines (from manifest or code) will make this actitivies not to stay on android stack, so when your MainActivity get result back, it will be result from last DialogActivity and than depending on result recieved you can either finish() or continue with execution of MainActivity..
when u start activities like this there is no need to call finish() to destroy them, u just start new activity and they will be gone from stack. Of course, in your case, last DialogActivity u will start with StartActivityForResult() and as I explained in previous paragraph MainActivity will do something based on received results.
However making user goes through these dialogs several times at the beginning application, is not something I would consider good practice and it can make your user just give up and go for some less annoying app. (don't get this wrong, it's just my advice to rethink about concept)
Hope you will solve it ;) Cheers
If you know about ActivityforResult then way is easier for you, First you need to start the dialog activity with method startActivityforResult... and then when dialog activity get close by back button you have to close it by Set result. In OnactivityResult method of start activity have to detect the same and close the same if setResult is not as according. Hope you got the point.
I would like to create a button in my game for the user. The button will quit the application.
Please can someone tell me if there is a way to do this.
Thanks.
Edit:
I have an activity which uses another activity with a class that extends Android.app.Application using set and get methods.
Simply using the back button switches the activities until it goes to the beginning.
I go in between these classes 20 times.
Thats why I needed a back button. But I guess there isn't so I will have to do it the long way and set everything back to the first state on quit. Thanks
There is not a way to make quit button. And there is good reason for that because the Android experience is having the back button do the closing. So you just to make the back button exit back to the home page. To do that you need make sure that your current activity is the only one oh the history stack. Then you can create a button that just calls finish(). Hope the detail explanation helps.
You probably want to mange the activity stack better.
If you look at Activity and Task Design Guidelines
it might help.
Setting the flags when you start each activity is probably the key, code such as
Intent i = new Intent(this, Whatever.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
will limit the number of instances of 'whatever' to one only. ( A different flag might be more appropriate for you depending on how you want your app to run, read up about them all)
Try this:
public void quit(View view) {
if(Build.VERSION.SDK_INT>=16 && Build.VERSION.SDK_INT<21){
finishAffinity();
} else if(Build.VERSION.SDK_INT>=21){
finishAndRemoveTask();
}
}
If I read your full question, you are looking for a Reset button not exactly a quit button. I had a similar issue... the next and previous takes only one step back at a time. I wanted to go back to the very beginning. The way I acheived this is to have a class to manage the pseudocursor.. basically an int that represented which resource to pick (I used a singleton). In the main activity's Menu (android.view.Menu), I added a reset/go to beginning option. This will simply reset the pseudocursor. In my activity class's onResume(), I had the code to get the resource from the singleton. So no extra coding was required there.
Instead of having this option under Menu, you can always have a button in UI which does the same thing.