I'm writing an app that has multiple activities. Activity A calls Activity B, not expecting a result. Then if a button is pressed B startsActivityForResult with Activity C. When Activity C is done, it makes an intent with all of the extras it needs and finishes. The problem is that when it calls this.finish() or just finish(), it brings me all the way back out to Activity A. onActivityResult in Activity B is not called. What is wrong?
Activity A: Starts Activity B
Intent in = new Intent(ccstart.this,mainmenu.class);
in.putExtra("uid",loginresponse);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("usr",text_user.getText().toString());
// Commit the edits!
editor.commit();
startActivity(in);
Activity B: Starts Activity C for result
Intent intent = new Intent(mainmenu.this,filebrowser.class);
startActivityForResult(intent,0);
Activity C: Return statement
Intent result = new Intent();
result.putExtra("fname", file.getAbsolutePath());
this.setResult(Activity.RESULT_OK, result);
finish();
Activity B: Upon the result of activity c...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// If the request went well (OK) and the request was PICK_CONTACT_REQUEST
if (resultCode == Activity.RESULT_OK && requestCode==0) { //upload a file
final String fname = data.getExtras().getString("fname");
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); //Load settings
final String uid = settings.getString("uid", "");
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
doFileUpload(fname, uid);
}
}).start();
}
}
What is the issue with that? It happens with an activity that doesn't return a result as well, so its not just this one.
Thanks!
You need to explicit close your activity when you start the next one, if not, it stays in the "stack of activities" that you can access with the back button or when the next activity closes.
You need to call finish on activity A after you started activity B
Related
I've 2 activities, and need the main Activity to send some data to the second Activity then the second activity analyze this data and send response back, so I've the below 2 codes:
Main Activity:
import static tk.zillion.mobile.SecondActivity.EXTRA_STUFF;
public class MainActivity extends Activity {
private static int PICK_CONTACT_REQUEST = 0;
private static final int SECOND_ACTIVITY_RESULT_CODE = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start the SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, "my text");
startActivityForResult(intent, SECOND_ACTIVITY_RESULT_CODE);
finish();
}
// This method is called when the second activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "I'm the Main activity", Toast.LENGTH_SHORT).show();
// check that it is the SecondActivity with an OK result
if (requestCode == SECOND_ACTIVITY_RESULT_CODE) {
if (resultCode == RESULT_OK) {
// get String data from Intent
String returnString = data.getStringExtra(EXTRA_STUFF);
// set text view with string
Toast.makeText(this, "I'm the Main activity", Toast.LENGTH_SHORT).show();
}
}
}
and the Second Activity is as below:
public class SecondActivity extends Activity {
static final String EXTRA_STUFF = "tk.zillion.mobile.EXTRA_STUFF";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
String s = getIntent().getStringExtra(Intent.EXTRA_TEXT);
Toast.makeText(this, "I'm the second activity " + s , Toast.LENGTH_SHORT).show();
Bundle basket =new Bundle();
basket.putString(EXTRA_STUFF, s);
intent.putExtras(basket);
setResult(RESULT_OK, intent);
finish();
}
}
The second activity is fired correctly, and receive the data from the main one, but once the data is sent back the onActivityResult is not fired!!
How can I solve it? thanks
Remove the finish() call that comes after startActivityForResult(). You prevent the oncoming activity to give a result back, because you remove your MainActivity with finish() from the back task.
Dont call finish() after startActivityForResult otherwise that instance of MainActivity that started the second one will be destroyed
I'm wondering, is it possible to send information to the activity that I return to after calling finish()?
For example, I have an Activity SendMessageActivity.class which allows the user to post a message to their feed. Once that message has been saved to the server, I call finish(). Should I instead just start my MainActivity.class with a new Intent? Or is it better for life cycle development to just finish SendMessageActivity.class?
I don't see the point of starting a new activity since closing the current one will always bring you back to MainActivity.class. How can I just send a String extra after finishing the current Activity?
Use onActivityResult.
This might help you to understand onActivityResult.
By using startActivityForResult(Intent intent, int requestCode) you can start another Activity and then receive a result from that Activity in the onActivityResult() method.So onActivityResult() is from where you start the another Activity.
onActivityResult(int requestCode, int resultCode, Intent data) check the params here. request code is there to filter from where you got the result. so you can identify different data using their requestCodes!
Example
public class MainActivity extends Activity {
// Use a unique request code for each use case
private static final int REQUEST_CODE_EXAMPLE = 0x9988;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an Intent to start AnotherActivity
final Intent intent = new Intent(this, AnotherActivity.class);
// Start AnotherActivity with the request code
startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
}
//-------- When a result is returned from another Activity onActivityResult is called.--------- //
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// First we need to check if the requestCode matches the one we used.
if(requestCode == REQUEST_CODE_EXAMPLE) {
// The resultCode is set by the AnotherActivity
// By convention RESULT_OK means that what ever
// AnotherActivity did was successful
if(resultCode == Activity.RESULT_OK) {
// Get the result from the returned Intent
final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA);
// Use the data - in this case, display it in a Toast.
Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
} else {
// AnotherActivity was not successful. No data to retrieve.
}
}
}
}
AnotherActivity <- This the the one we use to send data to MainActivity
public class AnotherActivity extends Activity {
// Constant used to identify data sent between Activities.
public static final String EXTRA_DATA = "EXTRA_DATA";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
final View button = findViewById(R.id.button);
// When this button is clicked we want to return a result
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Create a new Intent as container for the result
final Intent data = new Intent();
// Add the required data to be returned to the MainActivity
data.putExtra(EXTRA_DATA, "Some interesting data!");
// Set the resultCode to Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(Activity.RESULT_OK, data);
// With finish() we close the AnotherActivity to
// return to MainActivity
finish();
}
});
}
#Override
public void onBackPressed() {
// When the user hits the back button set the resultCode
// to Activity.RESULT_CANCELED to indicate a failure
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
}
Note : Now check in MainActivity you startActivityForResult there you specify a REQUEST_CODE. Let's say you want to call three different Activities to get results.. so there are three startActivityForResult calls with three different REQUEST_CODE's. REQUEST_CODE is nothing but a unique key you specify in your activity to uniquely identify your startActivityForResult calls.
Once you receive data from those Activities you can check what is the REQUEST_CODE, then you know ah ha this result is from this Activity.
It's like you send mails to your lovers with a colorful covers and ask them to reply in the same covers. Then if you get a letter back from them, you know who sent that one for you. awww ;)
You can set result of an activity, which allow you to data into an initent.
In your first activity, call the new one with startActivityForResult() and retrieve data in method onActivityResult. Everything is in documentation.
try this:
in First Activity:
Intent first = new Intent(ActivityA,this, ActivityB.class);
startActivityForResult(first, 1);
Now in Second activity: set Result during finish()
Intent intent = new Intent();
intent.putExtra("result",result); //pass intent extra here
setResult(RESULT_OK,intent);
finish();
First activity Catch the result;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 1
if(requestCode==1)
{
String message=data.getStringExtra("result");
//get the result
}
}
If you call finish() to avoid that the user go back to SendMessageActivity.class, you can set this flags to your intent:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
This will open the MainActivity and remove the SendMessageActivity from the activities stack.
I want to accomplish this structure:
Activity A is showing. If some button is pressed then open activity B (without closing current instance of A). If I press a back button of B I want just to finish B so that I can see my old instance of A. But if in activity B I press another button, I want to close A and open C.
How can I close activity A and start activity C when activity B is opened?
Explanation: When B is active the A mustn't be destroyed so that I could return to it. But if I want to open C then A must be destroyed, so that if I'd press back button of C I wouldn't see it anymore.
I already implemented the code that opens all of the activities by using startActivity() and finish() methods. All I need right now is an answer or suggetion of how could I rework my structure to accomplish my goal.
EDIT
I think I've got an idea to use startActivtyForResult() when I want to open B, so that when I'm ready to open C I'd just let A do this with closing itself.
When you Press Button C go to the ActivityC you just need to pass addFlag method with intent as follows
public void onClick(View v) {
if(v.getId()==R.id.butoonC){
Intent intent = new Intent(this, ActivityC.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
here Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all the activity from activity stack except activity B and activity C. So when u backpress from Activity B you Activity will not able to go back to Activity A.
I hope this work for u
Here is how I've solved the problem:
Activity A:
//Start Activity B
startActivityForResult(new Intent(this, B.class), 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
startActivity(new Intent(this, C.class));
finish();
}
}
Activity B:
//back button press:
setResult(RESULT_CANCELED, new Intent());
finish();
//start Activity C button:
setResult(RESULT_OK, new Intent());
finish();
Hope it will help someone.
I use this and worked for me
in Activity A when a button press to go to Activity B use this code :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
ActivityA.this.finish();
}
});
It is close Activity A and when back button pressed do not come back to this Activity.
I have the following situation:
Activity A starts Activity B, by using startActivityForResult
Activity B then returns an ArrayList of Strings to Activity A by using on finish().
Here is a code example of what exactly Activity B does:
ArrayList<String> urls = new ArrayList<>();
urls.add("Some string");
Intent intent = new Intent();
intent.putStringArrayListExtra(KEY, urls);
setResult(Activity.RESULT_OK, intent);
finish();
Then Activity A receive the data in onActivityResult(...)
The issue I have is that when the user taps the done button and Activity B's code example executes, Activity B freezes for about 3 seconds (when I have about 2 strings in the ArrayList). The more strings I have in the ArrayList the longer it freezes. I have more or less determined that it is finish() that causes the UI thread to freeze.
Is there a way to call finish() without freezing Activity B? If not, why is this happening?
EDIT:
Here is the full example:
/**
* Background task
*/
private class gatherUrlsTask extends AsyncTask<ArrayList<PictureEntry>, Integer, Intent> {
#Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
bt_done.setVisibility(View.GONE);
fab_add_picture.setVisibility(View.GONE);
}
#SafeVarargs
#Override
protected final Intent doInBackground(ArrayList<PictureEntry>... params) {
ArrayList<String> imagePaths = new ArrayList<>();
for (PictureEntry pictureEntry : params[0]) {
if (pictureEntry.isSelected()) {
imagePaths.add(pictureEntry.getPath());
}
}
if (imagePaths.size() == 0) {
Toast.makeText(getApplicationContext(), R.string.please_select_atleast_one_image, Toast.LENGTH_LONG).show();
return null;
} else {
Intent intent = new Intent();
intent.putStringArrayListExtra(SELECTED_IMAGES_KEY, imagePaths);
return intent;
}
}
#Override
protected void onPostExecute(Intent intent) {
setResult(Activity.RESULT_OK, intent);
finish();
}
}
However I can remove everything from the AsyncTask since it did not have any effect on performance.
i don't know what is cause of that, but for prevent ui getting freezed, use asyncTask and then in the onPostExcecute call finish()
I have a main_activity calling activity B. Activity B will update a few things on main_activity, while updating a few things on itself. A back button press on activity B will bring it back to main_activity. I managed to keep activity B's contents intact, but not the main_activity (being recreated). I referred to this SO question.
What I want is to keep both main_activity's and activity B's contents intact. How can I achieve that?
This is my code:
main_activity calls activity B on menu select:
Intent mIntent = new Intent(mainActivity.this, Activity_B.class);
//startActivityForResult(mIntent);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(mIntent);
activity-B back press:
#Override
public void onBackPressed() {
Intent mIntent = new Intent(Activity_B.this, mainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
startActivity(mIntent);
}
You can save contents of MainActivity that you want to update in ActivityB using SharedPreferences and in MainActivity get the value from SharedPreferences and display.
//Save something in Activity B
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor e = settings.edit();
e.putString("variableName", "a string value");
e.commit();
//Retrieve in MainActivity
String mContent = settings.getString("variableName", "");
SharedPreferences will store the value even if you exit the app. You can also access the values stored in SharedPreferences from any other class.
U need to start your activity from main activity as below
Intent mIntent = new Intent(mainActivity.this, Activity_B.class);
startActivityForResult(mIntent,PICK_CONTACT_REQUEST(int));
**in activity B ( for set result to update your main activity by Intent ) **
#Override public void onBackPressed() {
Intent mIntent = new Intent ();
mIntent.putExtra("some_key",someData);
mIntent.putExtra("Some_mey2",someData2);
setResult(RESULT_OK,mIntent);
}
**handling result in your main_activity from activity b and update ui of main_activity ** `
#Override
protected void onActivityResult(int. requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful
if (resultCode == RESULT_OK) {
someTextfield.settext(data.getStringExtra("Some_key");
}
}
}