naveen babu
naveen babu

Reputation: 33

How to pause an Activity till other Activity returns a result?

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"

Upvotes: 1

Views: 1381

Answers (1)

mariosmant
mariosmant

Reputation: 454

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.

Upvotes: 1

Related Questions