简体   繁体   English

通过活动传递和保存数据

[英]Passing and keeping data through activities

I have three activities. 我有三个活动。

  • Settings activity [A] // where you input a number that will be used in the main activity 设置活动[A] //您在其中输入将在主要活动中使用的数字
  • Main activity [B] // where it uses the number inputted by the settings activity 主要活动[B] //使用设置活动输​​入的数字
  • Pause activity [C] // which is dialog that pauses the main activity and gives you the option to reset it 暂停活动[C] //,这是一个对话框,可暂停主要活动并为您提供重置活动的选项

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 CI have tried some methods but i cant make it work. 我的问题是,我找不到将该数字从A传递到B的方法,并且当用户从CI重置B时要保存该数字的方法已经尝试了一些方法,但我无法实现。 My code: 我的代码:

A: A:

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

B: B:

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

    }

C: C:

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

Thanks for any help 谢谢你的帮助

您可以在启动暂停活动C时使用startActivityForResult()。startActivityForResult()使您可以将数据发送到接收活动,在您的情况下为MainActivityC。请参见此处的教程。

Fixed it. 修复。 New code: 新代码:

A: 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: 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: C:

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

You can also set up a (Data)Fragment for exchanging data? 您还可以设置一个(Data)Fragment来交换数据吗?

in its onCreate set setRetainInstance(true); 在其onCreate中设置setRetainInstance(true);

ad some setter/getters to it, for your card2string 在您的card2string上添加一些设置器/获取器

and in the different Activities get an instance of it through: 并在不同的活动中通过以下方式获得它的实例:

in eg MainActivity 在例如MainActivity

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

and in the eg onCreate or where you need the data: 以及在onCreate或需要数据的地方:

 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 确保在DataFragment中覆盖

    @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... 并在onCreate中使用捆绑包保存和恢复您的数据,以防Fragment被破坏...

HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM