I have this issue that I have a list of A and I can add new A, so I startactivityforresult activity with form for A and there is also a button for selecting AType, so I startactivityforresult another activity with AType list.
Problem comes when I do post back result in AType list.
Intent intent = new Intent();
intent.putExtra("aType", aType);
setResult(RESULT_OK, intent);
finish();
And the result is pushed back to A List instead of A Form.
Is this normal behavior or I am doing something wrong?
If this is normal behavior, what can I do to push back result to A Form instead of A List.
If you need more code - I will provide, but I find it now irrelevant.
I have found out that A Form finishes after startactivityforresult call. But why?
Activity1:
Intent intent-=new Intent(this,myclass.class);
startActivityforResult(intent,100);
Override the method OnACtivity result in Activity 1
Activity2:
setResult(RESULT_OK);
finish();
Turned out I have noHistory="true" in AndroidManifest.xml for activity A Form, that is why it was going back to A List Activity
Related
This question already has answers here:
Android: Go back to previous activity
(24 answers)
Closed 3 years ago.
I have two activities, How is it possible to go back to a previous activity.
What code do I need to go back to previous activity
Assuming you started the new activity with
startActivity(new Intent(this, SecondActivity.class));
puts the SecondActivity in front and the FirstActivity in the backstack. To go back to the FirstActivity, put this in your second activity.
finish();
If you start the activity with result like below :
From 1st Activity :
startActivityForResult(new Intent(this, SecondActivity.class),requestCode);
You can finish the activity with required intents :
From 2nd Activity :
// Optional if you want to pass values from second activity to first
Intent data = new Intent();
data.putExtra("key","any_value");
setResult(RESULT_OK,data);
// Just finish
finish();
Refer the below link for more information like onActivityResult callback and more
https://developer.android.com/training/basics/intents/result
1st Method
If you are using Intent then make sure you don't call finish(); method after using the following code,
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
By this code, the Back Button you created will do its work and go to previous Activity.
And also don't override the onBackPressed() method in Activity.
2nd Method
There is another way that you can achieve this by setting Home Button to go to specific Activity, For that, you have to create Back Button in Action Bar in onCreate() method.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
And in the AndroidManifest.xml file, you have to add the following,
<activity
android:name="com.example.yourapplication.SecondActivity"
android:label="#string/title_second_activity"
android:parentActivityName="com.example.yourapplication.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.yourapplication.MainActivity" />
</activity>
using intent
intent = new Intent(this, SecondActivity.class);
startActivity(intent);
Or you can use onBackPressed() if the activity added to backstack
onBackPressed();
I have 2 Activities, say activity A and activity B. I have to call activity B from the activity A. Now that is done by using Intent. There is some code in the activity A that must be executed after activity B ends. How can that be done?
I use the following code :
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
finish();
How can i accomplish that?
According to Android Docs: "Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity())."
Activity For Result Doc
And here you have a question about Activity For Result where you will find an example.
Consider scenario 1:
I have activity and launch another activity to do something. After 2nd activity done (action complete) - I need to update 1st activity. I understand this can be done using "launch for result"
Scenario #2:
I have service that runs on background, fetches data, etc. This also can update 1st activity. In this case activity can be live or it can be paused too.
In iOS I just use NotificationCenter and it works. How can I "register" to receive events in Activity and make sure it is updated even if activity sleeps at that moment? I was thinking about creating global flags and check them onResume, but not sure what is the proper way to handle this scenarios most proper and "loose" way?
There are two common options.
LocalBroadcastManager is provided by the compatibility library and will allow Android components in the same app to send and receive messages in the form of Intents. It's a lot like normal broadcasts.
You can also find a third party "event bus" library, which will be a little more flexible.
You are correct, you would use startActivityForResult() when starting your second activity. Then, in your first activity, override onActivityResult() which you will put logic in to receive the result from the second activity. For example:
First Activity
int REQUEST_CODE = 1;
Intent newActivityIntent = new Intent(this, SecondActivity.class);
startActivityForResult(newActivityIntent, REQUEST_CODE);
then override onActivityResult()
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// Make sure the result is OK.
if (resultCode == RESULT_OK) {
// Make sure this result is from the corresponding request.
if (requestCode == REQUEST_CODE) {
// Extract your data from the intent.
MyData myResultData = data.getExtra("my_extra"); // Generic example.
}
}
}
In the second activity you simply gather the information needed to send back when the user is finished, and put it in the return intent.
Second Activity
Intent returnIntent = new Intent();
returnIntent.putExtra("my_extra", data);
// Now set the result.
setResult(RESULT_OK, returnIntent);
// Call finish, and the return intent is passed back.
finish();
This is probably the easiest and least complex way to achieve what you want. This way, you know the activity receiving the result will always be unpaused when it gets the result.
I have been spending lots of hours figuring the reason why top of the stack is not cleared yet.
Well I tried the following:
Intent intent = new Intent(ActionBarActivity.this, MainActivity.class);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And it turned me to the MainActivity. Then when I try to press back button, the program does not exit, but instead it turns me to the first page of ActionBarActivity.
Let me be more specific:
In MainActivity I call ActionBarActivity. In the ActionBarActivity I have a search bar and I enter a query there and just print the value of the query.
If you think how it will work is a below:
MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3 -> ..
In that ActionBarActivity I have also an option which brings me back to the MainActivity.
So as I said when I run application with the way above, it will bring me to the MainActivity.
Nice so far, but when I press the back button I expect it to exit, but instead it goes to ActionBarActivity1. I doubt the stack is not properly erased.
What should I do in this case. Thanks
I'm assuming you've seen this post: Android: Clear the back stack and it is not what you want
The FLAG_ACTIVITY_NEW_TASK is creating an new task stack. When you hit "back" that is the only activity in that stack, so Android goes to the previous task - which has the calling activity at the top.
If you remove that flag, then MainActivity is already in the task stack and is brought to the top. And "back" removes it and brings you back to the calling activity. So far, that is the expected Android behavior.
EDIT: Since you commented that you only have 2 activities, then one option is to "finish()" ActionBarActivity after it starts MainActivity like this:
startActivity(intentMainActivity);
finish();
To get the behavior you want with many activities or only in certain situations, you need to use SET_RESULT and finish the activities in the stack when you hit the button to go to the MainActivity. In other words, Android assumes you want to keep activities even when new ones start. But in this case you don't, so you have to tell the activity to destroy itself.
i.e. call setResult(DONE) and then use startActivityForResult and check for your "DONE" flag in the onActivityResult method.
then this:
MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3
-> MainActivity
becomes:
MainActivity -> ActionBarActivity1 -> ActionBarActivity2 -> ActionBarActivity3
-> call "Done"
Destroy ActionBarActivity3
Destroy ActionBarActivity2
Destroy ActionBarActivity1
MainActivity remains
For example, in ActionBarActivity2:
startActivityForResult(intentActionBarActivity3, ActionBarActivity2_ID);
Then in ActionBarActivity3:
public void exitButton(View view) {
setResult(MY_DONE_FLAG);
finish();
}
And back in ActionBarActivity2:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ActionBarActivity2_ID && resultCode == MY_DONE_FLAG) {
setResult(MY_DONE_FLAG, data);
finish();
}
}
And so on with all activities that you want to "close" when that button is pressed.
try
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
insetead of
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
and you should also use it with every intent which leads you to ActionBarActivity.
i mean if you are going back to ActionBarActivity form ActionBarActivity1 then you use it with intent.
then create intent for MainActivity in your so called ActionBarActivity.
Intent intent = new Intent(ActionBarActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Good luck !!!
Say I have 2 activities (ActivityOne and ActivityTwo). How would I call ActivityTwo from ActivityOne? Then how would I return to ActivityOne from ActivityTwo? For example, I have a listview with all the contacts on the host phone. When I tap on a contact, another activity shows information and allows editing of that contact. Then I could hit the back button, and I would go back to the exact state that ActivityOne was in before I called ActivityTwo. I was thinking an Intent object, but I am not sure. Could someone post some code?
In your first activity (AcitvityOne) you could call
Intent intent = new Intent(this, ActivityTwo.class);
startActivityForResult(intent, CODE);
Then you can override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
to receive the result of this activity
In your example, it seems like you will want to pass data between Activity One and Activity Two (namely, the reference of the contact you want to edit), so you should use a Bundle with your intent in Activity One.
Intent myIntent = new Intent(this, ActivityTwo.class);
myIntent.putExtra("CONTACT", position);
this.startActivity(myIntent);
"CONTACT" is a key and position is a value (position would have been defined before, it could be whatever you want it to be, for example an integer. The way you retrieve it is slightly different based on what type it is, note the getInt() in Activity Two code below).
And in Activity Two, in onCreate()
Bundle extras = getIntent().getExtras();
if (extras != null) {
contact = extras.getInt("CONTACT");
/**Do what you want with the integer value you have retrieved**/
}
As for your other question, the back button removes the current Activity from the top of the tasks stack and therefore, you return to the previous Activity, normally in exactly the same state as you have left it.
However, if the phone is running low on memory, Android might kill off a process or Activity so it is possible in theory that your previous Activity will not show in exactly the same state as you have left it. If you have any important data, you should save it before launching Activity Two. If you just want to return to Activity One in the same state for user friendliness, then it's fine to rely on the back button.