0

Background:
If you have mainactivity that has a datamember named points and you want to send it to secondactivity by pressing a button.

When you are inside of second activity and you have transferred data points from mainactivity to secondactivity's datamember named points.

In secondactivity you add some more points and suddenly you click on the button back navigation button and you go back to the mainacitivty page.

Problem:
The main problem is that HOW should I retrived the new points from secondacitivity to the mainactivity when you press on the button back navigation.

Thank you!

Back navigation button
Back navigation button

Info:
*Im new in Android.

HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
  • this very simple just view this link [https://stackoverflow.com/a/14292451/6538486](https://stackoverflow.com/a/14292451/6538486) – Uthaya Feb 01 '18 at 07:54

5 Answers5

0

You can use this:

@Override
public void onBackPressed() {
 //write code here where you want to send data to another activity
}

On how can you retrieve it? you can do something like this:

1) create a POJO class:

public class Info {
 private string someData;

    public static Info getInstance() {
        if (info == null) {
            info = new Info();
        }
        return info;
    }

public String getsomeData() {
        return someData;
    }

public String setsomeData(String someData) {
        this.someData = someData;
    }
}

2) and then inside you on backPressed event you can do something like this:

Info.getInstance.setSomeData(yourData);

3) and to get this data in desired activity you can do:

Info.getInstance.getSomeData();
3iL
  • 2,146
  • 2
  • 23
  • 47
0

You can use this step Fisrt Activity declare startActivityForResult instead of startActiviy

    Intent askIntent = new Intent(FirstActivity.this, SecondActivity.class);

            // Start SecondActivity with the request code
            startActivityForResult(askIntent, 111);

OnActivityResult declare firstActivity

      @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 1
    if (requestCode == 111) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            final String result = data.getStringExtra(SecondActivity.Result_DATA);

            // Use the data - in this case display it in a Toast.
            Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
        }
    }
}

SecondActivity OnBackpress

      Intent sendIntent = new Intent();

            // Add the required data to be returned to the FirstActivity
            sendIntent.putExtra(Result_DATA, "Anders Hejlsberg");

            // Set the resultCode to Activity.RESULT_OK to
            // indicate a success and attach the Intent
            // which contains our result data
            setResult(RESULT_OK, sendIntent);

            // With finish() we close the SecondActivity to
            // return to FirstActivity
            finish();
mehul chauhan
  • 1,792
  • 11
  • 26
0

You can just override onBackPress() in SecondActivity and start it with startActivityForResult(). In onBackPress() you send your data back to MainActivity.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
0

If in your case data is the prime requirement then I will not recommend you to pass the data here and there. What I suggest you is that you just add the data from MainActivity to SharedPreference and go to SecondActivity and again retrieve the data from SharedPreference (Which you added previously from MainActivity) and if you want you can again add more points in SharedPreference and go back to MainActivity and you can again retrieve the data from SharedPreference.

Here the data would be updated from the SecondActivity by the help of SharedPreferenec. By using SharedPreferenec you will have no headache of passing the data from one Activity to other and also Application will not take the load.

Pranay Soni
  • 403
  • 3
  • 12
0

You can use this library

https://github.com/greenrobot/EventBus

Create a pojo class and pass it through the Eventbus post method

theanilpaudel
  • 3,348
  • 8
  • 39
  • 67