每当我开始新活动时,ArrayList的共享首选项都会被覆盖

时间:2019-07-05 10:36:26

标签: java android android-intent arraylist sharedpreferences

我有一个字符串数组列表,MainActivity读取该字符串,然后将其转换为Gson,然后该应用程序继续进行下一个活动(history.class活动),然后通过检索将其打印出来。我实现了一个返回MainActivity的按钮,但是每次我回到MainActivity时,arraylist都会被覆盖,有没有办法克服这个问题?另外,我不想使用close()finish()作为返回MainActivity的方式。

MainActivity.class

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ArrayList<String> arrPackage;
        setContentView(R.layout.activity_main);
        final SharedPreferences sharedPreferences = getSharedPreferences("USER",MODE_PRIVATE);
        final EditText address = findViewById(R.id.address);
        final TextView result = findViewById(R.id.result);
        Button save = findViewById(R.id.button);

        arrPackage = new ArrayList<>();
        final Intent next = new Intent(this,history.class);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(address.getText().toString().isEmpty()) {
                    Toast.makeText(MainActivity.this,"Plz Enter all the data",Toast.LENGTH_LONG).show();
                }else{
                    String addressData = address.getText().toString().trim();
                    arrPackage.add(addressData);
                    Gson gson = new Gson();
                    String json = gson.toJson(arrPackage);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("Set",json );
                    editor.commit();
                }
                startActivity(next);
            }
        });
    }
}

History.class

public class history extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        Button back = findViewById(R.id.back_btn);
        TextView result = findViewById(R.id.tv_history);
        final Intent back_activity = new Intent(this,MainActivity.class);
        final SharedPreferences sharedPreferences = getSharedPreferences("USER",MODE_PRIVATE);

        Gson gson = new Gson();
        String json = sharedPreferences.getString("Set", "");
        if (json.isEmpty()) {
            Toast.makeText(history.this,"There is something error",Toast.LENGTH_LONG).show();
        } else {
            Type type = new TypeToken<List<String>>() {
            }.getType();
            List<String> arrPackageData = gson.fromJson(json, type);
            StringBuilder builder = new StringBuilder();

            for(String data:arrPackageData) {
                builder.append(data + "\n");
            }
            result.setText(builder.toString());

        }
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(back_activity);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

arrPackage = new ArrayList<>();

此行覆盖您的arrayList。要保存您的arrayList,首先需要检查是否有数据保存在首选项列表中为

Gson gson = new Gson();
    String json = sharedPreferences.getString("Set", "");

    if (json.isEmpty()) {
        // runs when you app dont have data in shared preference initalize your arrayList here
        arrPackage = new ArrayList<>();
    } else {
        // when your app already stored list then read from the preference.
        Type type = new TypeToken<List<String>>() {
        }.getType();
        arrPackage  = gson.fromJson(json, type);
    }

答案 1 :(得分:0)

在“活动”条目内的AndroidManifest.xml中添加以下行:

android:launchMode="singleTask"

例如:

<activity
        android:name="your_package.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask">
</activity>

这样,您的MainActivity将成为单实例Activity,当您为其调用startActivity时,已经创建的Activity将成为前台。