StartActivityForResult go back - java

I am using StartActivityForResult for multiple activities. My main activity is where I initialize it. On my second activity I input some values and pass to a third activity. Now, When i'm on the third activity I want to be able to go back to the second activity if ever I want to edit the values i passed. What should I do?
MainAct.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
//Something
}
}
SecondAct.java
Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
vd2.putExtra("name",name);
vd2.putExtra("lname",lname);
vd2.putExtra("lnumber",lnumber);
vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(vd2);
finish();
ThirdAct.java
Intent intent = new Intent();
intent.putExtra("firstname",name);
intent.putExtra("lastname", lname);
intent.putExtra("licensenumber", lnumber);
setResult(Activity.RESULT_OK, intent);
finish();
How can I go back to second Activity from third activity to edit some values if ever?

You should not call finish() on second activity when starting the third one.
Then onActivityResult() will be call when third activity is finished.
Call
startActivityForResult(vd2);
instead of
startActivity(vd2);

simply remove finish();
Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
vd2.putExtra("name",name);
vd2.putExtra("lname",lname);
vd2.putExtra("lnumber",lnumber);
vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(vd2);
finish(); //remove this line
in this way when your third activity closes user will go back to 2nd one, also you should implement onActivityResult in your 2nd activity so you can handle weather user wants to edit or has finished and should go back to 1st activity! (i.e. setting the result of the intent came from 1st activity and finish the 2nd one!)
here is what I mean in code:
In your 2nd activity do this,
#override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
// user should have done his job on 3rd activity and we should finish the 2nd activity to go back to first activity!
}else{
//user still editing!
}
}
and instead of startActivity(vd2); do startActivityForResult(vd2);

Related

Can I pass an Intent extra on finish()?

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.

How to dismiss transparent Android activity and tap through to a second activity showing underneath it

I have two activities, one which is transparent with a second activity showing underneath it. Is it possible to tap the transparent activity, dismiss it and have the tap carry through to the activity underneath?
Not sure what you mean exactly by "carry through", but this is what you can do in Android.
Start Activity A. When starting Activity B, start as such:
Intent intent = new Intent(A, B);
startActivityForResult(intent, SOME_RESULT_CODE);
Then when you dismiss Activity B, aka call finish(), you need to make sure you send a result:
Intent goBackToActivityA = new Intent();
goBackToActivityA.putExtra(PUT_EXTRA_FOR_WHATEVER_YOU_WANT_IN_A);
setResult(Activity.RESULT_OK, goBackToActivityA);
finish();
And then when you get back to Activity A, you will need to capture that result and handle it:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SOME_RESULT_CODE) {
if(resultCode == Activity.RESULT_OK){
// extract PUT_EXTRA_FOR_WHATEVER_YOU_WANT_IN_A here
// whatever the "carry through" is you can manage here
}
}
}

StartActivityForResult, but activity finishes

I am trying to use Adobe Image Edit SDK to edit a photo then redirect to another activity, from my custom camera activity.
This works from another activity, simply by creating the Image edit Intent, using startActivityForResult, then treating the "Done" callback in said activity, in the method onActivityResult.
Intent imageEditorIntent = new AdobeImageIntent.Builder(mContext)
.setData(selectedImageUri)
.withToolList(tools)
.withOutput(new File(mLastSavedFilePath))
.build();
startActivityForResult(imageEditorIntent, 2);
and then
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 2) { // i get here
However, when I do this from my Custom Camera Activity, the activity ends when I click "done" in the image edit sdk (its onDestroy is called) before it gets to the result
Intent intent = FileUtils.getInstance().SavePhoto(data, mContext); //this returns an AdobeImageIntent
startActivityForResult(intent, 1);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//this never gets called, because activity finishes, but why?
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
So why is the activity finishing, if the other one does not?
Turns out this was my fault, I did not notice I had android:noHistory="true"
in the manifest for the second activity

Android, Get result from third activity

In first activity, there is empty ListView and Button.
When I press button, it starts second activity that has ListView of categories.
After I click into one of listElements it will start third activity that has ListView with elements that are belong to my chosen category.
When I choose element of third ListView it must send me back to first activity, where my chosen element is added to my empty ListView
Use Intent.FLAG_ACTIVITY_FORWARD_RESULT like this:
FirstActivity should start SecondActivity using startActivityForResult().
SecondActivity should start ThirdActivity using this:
Intent intent = new Intent(this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(intent);
finish();
This tells ThirdActivity that it should return a result to FirstActivity.
ThirdActivity should return the result using
setResult(RESULT_OK, data);
finish();
At that point, FirstActivity.onActivityResult() will be called with the data returned from ThirdActivity.
Though I'd implore you to change your architecture design, it is possible to do it like this:
File ActivityOne.java
...
startActivityForResult(new Intent(this, ActivityTwo.class), 2);
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null) {
//Collect extras from the 'data' object
}
}
...
File ActivityTwo.java
...
startActivityForResult(new Intent(this, ActivityTwo.class), 3);
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null) {
setResult(resultCode, data);
finish();
}
setResult(RESULT_CANCELLED);
}
...
File ActivityThree.java
...
//Fill the Intent resultData with the data you need in the first activity
setResult(RESULT_OK, resultData);
finish();
...

How to destroy previous activity

So I have 2 activities lets say A and B. A navigates to B, I want the activity A to be killed or make it unusable/unseen when directed from B.
so it should be like when I press the back button on B activity it should not open activity A instead it should go to the app tray.
also activity A should comeback when I clear the application data
thanks.
You could do this in one of two ways. First is finishing ActivityA so that it can't be resumed to later. When starting ActivityB from ActivityA, you'd do something like this:
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
this.finish();
Another way is to just finish ActivityA when it gets a result of any sort from ActivityB. This code would also be in ActivityA.
To start ActivityB:
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_ACTIVITYB);
To make sure ActivityA doesn't resume:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ACTIVITYB) {
finish();
}
}
REQUEST_ACTIVITYB is just an int of your choosing.

Categories

Resources