java - Do I need to include a default Intent in order to return to the previous Activity? -
I am working on a two to list Android application (this is for class assignment but it is not what I am asking about.) - As I was able to do as I could Trying to quit more code) The main screen displays a list of ToDo items with a button below to open a new toda item screen.
add new toDo items to the screen , There is a cancel button.
The relevant ToDoManagerActivity.java snippet Public void onCreate (Bundle savedInstanceState) {// Init and Setup Adapter, etc. FooterView.setOnClickListener (New OnClickListener) {Public Zero onClick (see v) {intent = new intent ( ToDoManagerActivity.This, AddToDoActivity.class); startActivityForResult (intent, ADD_TODO_ITEM_REQUEST);}}); // Attach the adapter to the list of activity in this list. Zero on scene safe; Activation code () // check result code and request code. The user presented // if a new ToDoItem // data intently build a new ToDoItem // and then add it to the adapter}
relevant AddToDoActivity.java snippet:
Secure vs. Create (Bundle Saved InstantState) {// Start the initial view, other events, etc.) Final button cancel button = button (button) Find ViewById (R.id.cancelButton); Cancel Button .Set onClicklist (New OnListList) {Click on Public Zero (see V) {setResult (RESULT_CANCELED, New Intent ()); Finish ();}}); }
The above code works. Previously, I was trying this in the onClick
cancelButton
:
; }
When I click the Cancel button, I could see that the onActivityResult
was being accessed in the log, but the screen back to the main ToDo Not returning the list screen.
Why does not the above code return to the previous screen, but the following code returns me to the previous screen? What are my misconceptions about work backstack / activities?
Public Zero onClick (see V) {setResult (RESULT_CANCELED, new intent ()); End(); }
According to:
public zero end ()
Call it when your activity is done and should be closed. ActivityResult is promoted back to anyone who launched you through onActivityResult ().
and
public null finishActivity (integer requestCode)
end any other activity that you have before startActivityForResult (intestate, int ).
You should call finish ()
to stop the current activity and FinishActivity ()
to close another activity. For which you have started using startActivityForResult (intent, intod code)
. Calling period ()
will not stop calling current activity.
In addition, there is no point in creating setResult for any
as you are not going back to any data It will be enough to: intent
()
setResult (RESULT_CANCELED); End();