my second activity is not returning result for first one

Home / Uncategorized / my second activity is not returning result for first one

Question:
I am trying to make an app in which MainActivity ask for result from Main2Activity code for MainActivity is:package hello.option_menu;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ye.collor.stackapplication.R;

public class MainActivity extends AppCompatActivity {
Button bt;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt=(Button)findViewById(R.id.bt);
TextView tv=(TextView)findViewById(R.id.tv) ;
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i=new Intent(MainActivity.this,Main2Activity.class);
startActivityForResult(i,1);
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1)
{
String message=data.getStringExtra("message");
tv.setText(message);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.jai, menu);

return true;
}

}

code of Main2Activity is:package hello.option_menu;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ye.collor.stackapplication.R;

public class Main2Activity extends AppCompatActivity {
Button bt;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt=(Button)findViewById(R.id.button);
et=(EditText)findViewById(R.id.et);
bt.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {

String message=et.getText().toString();
Intent intent=new Intent();
intent.putExtra("message",message);
setResult(1,intent);
finish();

}
});
}
public boolean onCreateOptionsMenu(Menu menu) {

menu.add("hello");

MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.jai,menu);

return true;
}

}

so the view of my app is:my MainActivity

my Main2Activity

So when I click the send button after typing the text in Main2Activity to set the text written in MainActivity it didn’t do that.It still remain "hello world",so text do not change .I am unable to trouble shoot this problem.


Answer:
In your MainActivity2 Change
setResult(1,intent);

to
setResult(RESULT_OK,intent);

What you are trying to do is set the hardcoded value of RESULT_TYPE as 1 which is the int value for RESULT_FIRST_USER whereas what you intended to do was set RESULT_OK

Also the Value of 1 that you are assuming would be requestcode in m=MainActivity2 is actually not REQUEST_CODE but RESULT_CODE. The requestCode remains in MainActivity and can be used to distinguish between different activities that you open.

Also you can change your onActivityResult as follows to keep it more safer
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1 && resultCode==RESULT_OK)
{
String message=data.getStringExtra("message");
tv.setText(message);
}
}

Update 1 Also as you mentioned it crashes after this, its because your TextView is not initialized properly in MainActivity.

So in MainActivity Change
TextView tv=(TextView)findViewById(R.id.tv) ;

to
tv=(TextView)findViewById(R.id.tv) ;

so that your Textview that you declared Globally is assigned the value when you return in OnActivityResult
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *