I'm calling startActivityForResult from a child activity to MainActivity but never pass for onActivityResult
secondActivty this is the call
private void bringMainActivityToTop() {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bun= new Bundle();
bun.putParcelable("value",new ListData("HelloDummy",longi,lati,"data"));
intent.putExtra("bundle",bun);
setResult(RESULT_OK, intent);
startActivityForResult(intent, 111);
}
And I want see the call in here on the MainActivity I don't care the code I just want to see the call.
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
System.out.println("HelloWorld");
Log.i(TAG,"HelloWorld");
}
and the manifest
<activity
android:name="com.test.mppqvat.activity.MainActivity"
android:label="#string/main_act_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
onActivityResult() belongs to your second activity (if its not then it must to), not the MainActivity. onActivityResult() method wont trigger in MainActivity just cuz MainActivity is started for result. Do you realize that?
Also your MainActivity if being started for result must SET the result using setResult() method and than it has to be finished using finish()method, that way the result will be passed from your MainActivity to your caller SeconActivity activity and its onActivityResult() will be fired.
MainActivity.java(in onCreate)
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
(outside onCreate)
#Override
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 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
//use resulted message
}
}
SecondActivity.java(in onCreate)
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});
Your called Activity must call setResult(Activity.RESULT_OK) or setResult(Activity.RESULT_OK, data)
Related
I would like to ask if there is a way to put Extras or Bundle when you call finish() method.
For example just like calling an Activity you can do this:
Intent intent = new Intent(view.getContext(), AddListing.class);
intent.putExtra("user_id", userID);
startActivity(intent);
is it possible to use it in finish()?
Thanks.
If you are looking to pass back results when an Activity is destroyed, what you should be looking for is : startActivityForResult.
There are two variants of startActivityForResult() , which allows to pass bundle as well in case of a necessity:
public void startActivityForResult (Intent intent, int requestCode)
public void startActivityForResult (Intent intent, int requestCode, Bundle options)
See this example to understand the concept better:
MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
}
// Call Back method to get the Message from other Activity
#Override
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 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
SecondActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText1=(EditText)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});
}
No finish() method is available with intent as it's parameter.If you want to pass some data back to calling Activity you can use setResult(int resultCode, Intent intent) before calling finish(). In this method you will need to pass bundle or extras in intent. And accept it in onActivityResult method in calling activity. This is the only way to pass data back to calling Activity. You can refer this link https://developer.android.com/training/basics/intents/result.html
Define the userId-->after the intent initiation ,
Example,
Intent i = new Intent(1st.this, 2nd.class);
String sDate = Dpcheckin.getText().toString();
String eDate = Dpcheckout.getText().toString();
String roomtext=RoomText.getText().toString();
i.putExtra("date1", sDate);
i.putExtra("date2", eDate);
i.putExtra("roomtext",roomtext);
startActivity(i);
where,
Mainly you need to define 2 classes inside the Intent,
1st one is the class from which you are sending the data, with(.this)extension.
2nd one is the class you are receiving the data , with (.class)extension.
I'm not sure if this is expected behavior, but if I do the following in OneActivity to launch TwoActivity:
Intent intent = new Intent(this, TwoActivity.class);
startActivityForResult(intent, RESULT_OK);
And in TwoActivity when I pass back to OneActivity:
Intent resultIntent = new Intent();
resultIntent.putExtra(SOURCE, TAG);
setResult(RESULT_OK, resultIntent);
finish();
With the above code and after overriding onActivityResult in OneActivity nothing happens. onActivityResult doesn't even seem to be called. If, however, I change RESULT_OK to 0, it works.
Is this expected? Has anyone else experienced that?
Check out the docs definition of the startActivityForResult method. It says:
requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits.
So your request code should be >= 0. If you check the value of the RESULT_OK response code, it's -1. It's important to note that the request code isn't the same as the result code. The request code serves you to identify the request the result is for, and the result code tells you if the request was successful or not.
You are confusing two different concepts:
The requestCode (the second parameter to startActivityForResult) is a unique ID you assign that can be any positive integer.
The resultCode (the first parameter to setResult) must be one of the constants in the Activity class as seen in the setResult documentation
You'll note that your onActivityResult receives both the requestCode you pass into startActivityForResult and the resultCode that you've set in setResult - make sure you are comparing the right numbers in your onActivityResult
please check your code with this sample:
MainActivity:
public class MainActivity extends Activity {
TextView textView1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
}
// Call Back method to get the Message form other Activity
#Override
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 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
}
and then SecondActivity:
public class SecondActivity extends Activity {
EditText editText1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText1=(EditText)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});
}
I have an activity A with this fuction:
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
checkSettings();
}
Is there any way to make the activity A to wait for the activity B ("Settings.class") finish to execute the fuction "checkSettings();" ?
Thanks
Edit: I may have misunderstood your question. If you want to run checkSettings() function in B then you need to define and call that function in your B activity.
Otherwise, if you want to wait for activity B to end before running checkSettings() then copy the following code.
In A:
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, 1);
}
then also in A Override onActivityResult method.. this gets called when B ends:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
checkSettings();
}
In your Activity A write
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, 100);
}
and also in you Activity A override onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(requestCode==100){
checkSettings();
}
}
}
and in your Activity B
when you want to finish that activity write that piece of code there
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Another approach to doing this since you are launching a separate activity is call check settings in onResume. This way since you're not having to pass any data and if the are other reasons you may want to refresh or recheck your settings, it will do so any time the activity is brought back to the top.
override fun onResume() {
super.onResume()
checkSettings()
}
I have an activity A that calls an activity B (FragmentActivity) using startActivityForResult(). Before activity B finishes by calling finish(), I use setResult(). In spite of all of this, onActivityResult() is not called. I'm not sure what I'm doing wrong as I'm not doing anything unconventional, or if this is an Android bug.
Seeing some of the suggestions on here, I've checked to see that I don't have android:launchMode="singleInstance" nor android:noHistory="true" set anywhere in my manifest file.
Calling activity B from A:
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivityForResult(intent, MY_RESULT);
Finishing activity B:
Intent intent = new Intent();
setResult(Activity.RESULT_OK, intent);
finish();
Overriding onActivityResult() in activity A:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
Toast.makeText(getApplication(), "toast", Toast.LENGTH_LONG).show();
}
Edit 1
I commented out nearly all of activity B's code (see below), leaving A's intact and the problem remained. So, A is definitely doing something strange that causes the problem. Worse, I have done this with other activities (using the same code) without any problems! A extends ExpandableListActivity, although I don't understand how any of that code would cause issues in onActivityResult(). Logcat isn't giving me anything useful.
This is the code tested for activity B, for posterity's sake (though it seems to be that the problem is in A):
public class ActivityB extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activityb);
Intent intent = new Intent();
intent.putExtra("data", "Hello from ActivityB");
setResult(Activity.RESULT_OK, intent);
finish();
}
}
try this and verify with your code:
ActivityA.java
public class ActivityA extends Activity{
private static final int MY_RESULT = 0;
private Button btnStart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
btnStart=(Button)findViewById(R.id.btnstart);
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, MY_RESULT);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplication(), data.getStringExtra("data"), Toast.LENGTH_LONG).show();
}
}
ActivityB.java
public class ActivityB extends FragmentActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Intent intent = new Intent();
intent.putExtra("data", "Hello from ActivityB");
setResult(Activity.RESULT_OK, intent);
finish();
}
}
Note : make sure you have added both the activity in your manifest file.
OK, thanks to everyone that pointed me in the right direction. I found the solution. The issue was that I was doing startActivityForResult() in a DialogFragment so I should have done
getActivity().startActivityForResult()
instead of just
startActivityForResult()
then it works as expected.
I have a fragment activity that displays an Android V2 Map. Inside I also have a onActivityResult used to handle the intent Extras that needs to be passed from the calling activity
public class DisplayMap extends FragmentActivity implements LocationListener {
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.map);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Log.v("TEST", "********************************************");
}
}
Here is the code form the activity that calls it.
Intent i = new Intent("com.example.DisplayMap");
setResult(RESULT_OK, i);
startActivityForResult(i, 2014);
But somehow the onActivityResult is not called inside.
Thanks in advance.
Dennis
onActivityResult() needs to be in the calling activity, it retrieves the result, as the name suggests.
To return a result from the called activity you'll need to use setResult() and finish that activity:
called activity:
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
with RESULT_OK being passed as the resultCode parameter to onActivityResult() and i as intent
calling activity:
Intent i = new Intent("com.example.DisplayMap");
startActivityForResult(i, REQUEST_CODE);
and to receive the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == REQUEST_CODE){
if (resultCode == RESULT_OK){
// RESULT OK, take the returned extras from intent and use them
}
}
}