Mrunal
Mrunal

Reputation: 628

how to get a result from current activity and then get back to the previous one

I have five activities for the different country.like, India, Sri Lanka, Argentina, America, China and each activity contains place picker activity to pick a current place.

When I press a button on the India activity, the place picker activity is launched.and pick the location and display this location on India activity.like this when i press a button on Argentina activity, the place picker activity is launched.and pick the location and display this location on Argentina activity.

for this, I want to send some data back to the previous screen. I used the shared preferences, but when I pick the location from any activity it always displays data in India activity instead of previous activity from which I launched the place picker activity.

in short, I need to get a result from this Place picker Activity and then get back to the previous one.

below is code where i send my data from place picker

   CharSequence city = place.getName();
   CharSequence cityAddress = place.getAddress();

            SharedPreferences settings = getSharedPreferences("city_address", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("city_address", (String) cityAddress);
            editor.putString("city_name", (String) city);
            editor.commit();

            Intent intent = new Intent(this, India.class);
            startActivity(intent);

and this is code in each activity to display data

  SharedPreferences settings = getSharedPreferences("city_address", Context.MODE_PRIVATE);
    String n = settings.getString("city_name", "");
    String a = settings.getString("city_address", "");
    cityname.setText(n);
    cetlocation.setText(a);

I've read about the StartActivityForResult() method, but I'm not yet sure how to use it properly, any examples?please help me I am new to the android studio.

Upvotes: 0

Views: 73

Answers (1)

Jyoti JK
Jyoti JK

Reputation: 2171

When I press a button on the India activity, the place picker activity is launched.and pick the location and display this location on India activity.

As you said, The place picker activity is to get data, And after getting data, You need to go back to Related Country activity and wants to display that details.

My suggestion is to use onResume() or startActivityforResult method.

Method 1

In your place picker activity

            SharedPreferences settings = getSharedPreferences("city_address", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("city_address", (String) cityAddress);
            editor.putString("city_name", (String) city);
            editor.commit();
            finish();

In your country activity,

 @Override
    public void onResume() {
       SharedPreferences settings = getSharedPreferences("city_address", Context.MODE_PRIVATE);
       String n = settings.getString("city_name", "");
       String a = settings.getString("city_address", "");
       cityname.setText(n);
       cetlocation.setText(a);
}

Method 2

In your country activity, when you are starting Place picker activity, use

Intent i = new Intent(this, Placepickeractivity.class);
startActivityForResult(i, 1);

And override onActivityResult method,

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == 1){
          String n = data.getStringExtra("city_name");
          String a = data.getStringExtra("city_address");
          cityname.setText(n);
          cetlocation.setText(a);

        }
    }
}

In your place picker activity, Send result as,

Intent returnIntent = new Intent();
returnIntent.putExtra("city_address", (String) cityAddress);
returnIntent.putExtra("city_name", (String) city);
setResult(1, returnIntent);
finish();

Upvotes: 2

Related Questions