1

I need to know how to pause an activity while the other activity sends a value. In my code the main activity calls second activity and the second activity sends the result so ideally the main activity should wait until it receives the result from second activity.

public class MainActivity extends AppCompatActivity {
public final static String key = "abc";
public final static String key1 = "abcd";
String abc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1 = (Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //   Toast.makeText(MainActivity.this, "sbabs", Toast.LENGTH_SHORT).show();
            send();

        }
    });
}

public void send() {
    //   Toast.makeText(MainActivity.this, "jsjaskb", Toast.LENGTH_SHORT).show();
    //  String str="this is";
    String[] str = {"Blue", "Green", "Purple", "Red"};
    Intent intent = new Intent(this, Second.class);
    intent.putExtra(key, str);
    startActivityForResult(intent, 1);


    Toast.makeText(MainActivity.this, ""+abc, Toast.LENGTH_SHORT).show();

    //  Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();

}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        abc = extras.getString(key1);
        //   txtinput1.setText(str);
        //   Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
    }
}

}

public class Second extends AppCompatActivity {
public final static String key="abc";
public final static String key1="abcd";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    Intent intent=getIntent();
    String[] msg=intent.getStringArrayExtra(key);
    Toast.makeText(this, "" + msg[0], Toast.LENGTH_SHORT).show();

    Button b1 = (Button) findViewById(R.id.button2);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //   Toast.makeText(MainActivity.this, "sbabs", Toast.LENGTH_SHORT).show();
            String str = "hello";
            Intent intent1 = new Intent();
            intent1.putExtra(key1, str);
            setResult(RESULT_OK, intent1);
            finish();


        }
   });
}

}

Now the problem is that the main activity prints str=null before second activity could assign the value str="hello"

MWiesner
  • 8,868
  • 11
  • 36
  • 70
  • What exactly is the problem? You can just move your Toast.makeText inside onActivityResult and it will print the correct value. – Wukash Mar 20 '16 at 15:01
  • you are already on right track. what's the problem now? – Vivek Mishra Mar 20 '16 at 15:08
  • you can do it with startActivityForResult .... Already answered http://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android – jonhid Mar 20 '16 at 15:16
  • Isnt there another way of doing it than calling it in the onActivityResult()?? because there is more atuff to be done and it has to be done on the same function – naveen babu Mar 20 '16 at 17:44

1 Answers1

1

Activity is paused automatically after starting a new Activity. But it must first exit the onClick() method you called. The abc holds the text you want. Toast is showing null because abc is null.

You have to do the whatever you want with the abc string inside the

onActivityResult

method, where it holds the correct value.

mantlabs
  • 352
  • 2
  • 6