simar
simar

Reputation: 1

How to transfer data from one time open activity to next

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();
    }

Upvotes: 0

Views: 44

Answers (1)

Apollo
Apollo

Reputation: 422

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 );

Upvotes: 1

Related Questions