Home » Java » java – Cant retrive Data from shared prefrence after close Activity-Exceptionshub

java – Cant retrive Data from shared prefrence after close Activity-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I cannot retrieve my saved data (Arraylist with Set ) from the shared preference when start or resume the activity, I put a Toast message to show up when open and close the activity to ensure the method is working, but still getting an empty results and my arraylist is empty and never saved any data when exit to the previus activity with even pressing back or pressing button to send me back , please find the codes below :

Note that I did open my app folder in the storage, and it is completely empty, the shared preference never saved in my phone, I need it to stay in the memory of the device until the user will wipe it with the button.

package com.cupheadcorp.domeno;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Activity_players_Score extends AppCompatActivity {



    ArrayAdapter<String> aa;
    ArrayList<String> data = new ArrayList<String>();
    ArrayList<String> dataold = new ArrayList<String>();
    Set<String> set = new HashSet<String>();
    SharedPreferences sp;






    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_players__score );




        Button backBtn = findViewById( R.id.backbtn );
        backBtn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent backintent = new Intent( Activity_players_Score.this , MainActivity.class );
                backintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                saveData();
                startActivity( backintent );
            }
        } );


        sp=getSharedPreferences("test", Context.MODE_PRIVATE);
        Button getScroreBtn = findViewById( R.id.getscoreBtn );
        getScroreBtn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ( !dataold.isEmpty())
                    {
                        String compartionSTR = dataold.toString();
                        if (!data.contains( compartionSTR )){
                            data.addAll( dataold );
                            data.add("got items from old ");
                        }

                    }

                else
                {
                    Bundle bundle = getIntent().getExtras();
                    String player1 = bundle.getString( "player1" );
                    String player2 = bundle.getString( "player2" );
                    String player3 = bundle.getString( "player3" );
                    String player4 = bundle.getString( "player4" );
                    data.add( player1 );
                    data.add( "new input" );
                    //data.add( player2 );
                    //data.add( player3 );
                    //data.add( player4 );
                }



                 //   if(!dataold.contains( data )) {
                 //       dataold.addAll( data );
                 //   }
                  //  else {
                 //       dataold.clear();
                 //   }


                    final ListView listView2 = findViewById( R.id.listteam1 );
                    aa = new ArrayAdapter<String>( Activity_players_Score.this, android.R.layout.simple_list_item_1, data );
                    listView2.setAdapter( aa );
                    aa.notifyDataSetChanged();
                    saveData();







            }
        } );



    }


    private void saveData(){

        SharedPreferences.Editor editor = sp.edit();
        if (set.isEmpty()) {
            Toast.makeText(Activity_players_Score.this, "saving to shared pref", Toast.LENGTH_LONG).show();

        }
        set.addAll(data);
        editor.putStringSet("key", set);
        editor.commit();



    }

    private void loadData() {

        sp.getStringSet( "key", set );
        dataold.addAll( set );
        if (dataold.isEmpty()) {
            Toast.makeText(Activity_players_Score.this, "loading from shared ", Toast.LENGTH_LONG).show();

        }

    }




    @Override
    public void onStart () {
        loadData();
        super.onStart();

    }

    @Override
    public void onStop () {
        saveData();
        super.onStop();

    }
    @Override public void onResume(){
        super.onResume();
    }

    public void loaditemlistview (){


    }












}
How to&Answers: