繁体   English   中英

如何将数据从一次打开的活动传输到下一次

[英]How to transfer data from one time open activity to next

我是 android 的新手。我在活动 1(一次打开的活动)中制作了微调器,并希望将用户从微调器中选择的值转移到活动 2。我曾经使用 sharedPreference 进行一次打开的活动。它只是第一次工作,但是第二次无法传输数据。我卡在这里请帮助我

    SharedPreferences Preferences=getSharedPreferences("PREFERENCE",MODE_PRIVATE);
    String FirstTime=Preferences.getString("FirstTimeInstall","");


       button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String parent=sp_parent.getSelectedItem().toString();
            String child=sp_child.getSelectedItem().toString();
            String college=sp_college.getSelectedItem().toString();

            Intent intent=new Intent(getApplicationContext(),Dashboard.class);
            intent.putExtra("parent",parent);
            intent.putExtra("child",child);
            intent.putExtra("college",college);
            startActivity(intent);
            finish();


        }
    });


            if (FirstTime.equals("Yes")){
            String parent=sp_parent.getSelectedItem().toString();
            String child=sp_child.getSelectedItem().toString();
            String college=sp_college.getSelectedItem().toString();

        Intent intent=new Intent(getApplicationContext(),Dashboard.class);
            intent.putExtra("parent",parent);
            intent.putExtra("child",child);
            intent.putExtra("college",college);
        startActivity(intent);
        finish();


    }else {
        SharedPreferences.Editor editor=Preferences.edit();
        editor.putString("FirstTimeInstall","Yes");
        editor.apply();
    }

开始新活动时,您可以通过意图发送数据。

Intent intent = new Intent( this, Activity2.class );
intent.putExtra( "key", "value" );
startActivity(intent)

在第二个活动中这样做:

getIntent().getStringExtra( "key" );

或者如果您发送 int 例如

getIntent().getIntExtra( "key", 0 );

暂无
暂无

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

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