0

I am new to android!. I have made spinner in activity 1 (one time open Activity) and want to transfer selected value from spinner by user to activity 2.I have used sharedPreference for one time open activity .Its only working 1st time but second time it cant transfer data.I am stuck here please help me

    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();
    }
simar
  • 1
  • 2
  • Have a look at this. https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – elbert rivas Jun 01 '20 at 06:58
  • i know intent method but in one time activity the data is transfer only first time – simar Jun 01 '20 at 07:08
  • get value from SharedPreferences in second activity, if is it defaultValue, get it from intent that received from first activity. what's the problem? – mohamad jalali Jun 01 '20 at 07:43
  • There are multiple ways, you can create your data holder class. and reference it from different activities for shared data. have a look here for multiple ways https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities – Umais Gillani Jun 01 '20 at 11:17
  • Intent used in shared preference is not taking value from activity1 .Its directly jumping to activity 2 – simar Jun 01 '20 at 20:01

1 Answers1

1

When starting a new activity you can send data through intent.

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

And in second activity do like this:

getIntent().getStringExtra( "key" );

or if you are sending int for example

getIntent().getIntExtra( "key", 0 );
Apollo
  • 420
  • 3
  • 6