-1

I have three activities.

  • Settings activity [A] // where you input a number that will be used in the main activity
  • Main activity [B] // where it uses the number inputted by the settings activity
  • Pause activity [C] // which is dialog that pauses the main activity and gives you the option to reset it

My problem is that i cant find a way to pass that number from A to B and that number to be saved for when the user resets B from C.I have tried some methods but i cant make it work. My code:

A:

long npsum = npint * 60000 + np3int * 1000;
Intent cardintent = new Intent(getApplicationContext(), card_game_2.class);
cardintent.putExtra("card2string",npsum);
startActivity(cardintent);

B:

Bundle card2extras = getIntent().getExtras();
    if (card2extras != null) {
        String startcardstring = card2extras.getString("card2string");
        startcard = Long.parseLong(startcardstring);

    }

C:

Intent resetintent = new Intent(card_2_pause.this, card_game_2.class);
startActivity(resetintent);

Thanks for any help

  • 2
    Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Lance Toth Mar 13 '18 at 09:36
  • 1
    Possible duplicate of [Android Shared preferences example](https://stackoverflow.com/questions/23024831/android-shared-preferences-example) – I am a Student Mar 13 '18 at 09:38

3 Answers3

4

you can use startActivityForResult() when starting Pause Activity C. startActivityForResult() enables you to send data to receiving activity which is, in your case MainActivity C. see tutorial here

pastillas
  • 124
  • 11
1

Fixed it. New code:

A:

 Intent timerdintent = new Intent(timer_settings.this, timer_2.class);
 Bundle timer2extras = new Bundle();
 timer2extras.putString("timer2string", String.valueOf(npsum));
 timerdintent.putExtras(timer2extras);
 startActivity(timerdintent);

B:

SharedPreferences pref = getApplicationContext().getSharedPreferences("timer2stringpref", MODE_PRIVATE);
    Bundle timer2extras = getIntent().getExtras();
    if (timer2extras != null) {
        String timer2string = timer2extras.getString("timer2string");
        starttimer = Integer.parseInt(timer2string);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("value", starttimer);
        editor.apply();

    } else {
        int starttimerx = pref.getInt("value", 0);
        starttimer = starttimerx;
    }

C:

Intent resetintent = new Intent(timer_2_pause.this, timer_2.class);
startActivity(resetintent);
0

You can also set up a (Data)Fragment for exchanging data?

in its onCreate set setRetainInstance(true);

ad some setter/getters to it, for your card2string

and in the different Activities get an instance of it through:

in e.g. MainActivity

  public final static String DATAFRAG = "DATA";//public so you can use it from any Activity

and in the e.g. onCreate or where you need the data:

 FragmentManager fm = getFragmentManager();
    dataFrag = (DataFragment) fm.findFragmentByTag(MainActivity.DATAFRAG);
    if (dataFrag == null)
    {
        dataFrag = new DataFragment();
        dataFrag.setCard2String("something");
        fm.beginTransaction().add(dataFrag, MainActivity.DATAFRAG).commit();
    }

make sure to override in the DataFragment the

    @Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
}

and in the onCreate use the Bundle to save & restore your data in case the Fragment was destroyed...

HTH

Noh Kumado
  • 1,551
  • 2
  • 9
  • 15
  • Could you be more specific plz cause when you say on Create id cant understand which activity you mean –  Mar 13 '18 at 11:03
  • the setRetainInstance stops the Fragment from beeing collected, so it need to be in the OnCreate of the Fragment, and to get the reference to the datafragment, well in each Activity that participates at the exchange, you need to call the Fragmentmanager to get it... best IMHO is to put that call in the onCreate, but you can also put it where you really need it , ah yes, and if you want to propagate back the event that the data changed, you will also need a Listener Interface, and in the DataFragment a listing with the listeners to be notified, beware tough in which thread you are in that case – Noh Kumado Mar 13 '18 at 11:24