首页 > 解决方案 > 后退按钮恢复修改后的文本视图

问题描述

我将简化我的代码来专门解决这个问题:

我有一个带有一些 TextViews 的活动 A,这些文本在活动的 OnCreate 方法中设置为存储在 SharedPreferences 中的某些键值。每个文本视图旁边都有一个按钮。单击按钮时,它会打开一个新活动 B,其中显示具有不同文本字符串的适配器。当用户单击一个时,新字符串存储在首选项中,并且用户通过意图被引导回活动 A,因此调用 OnCreate 方法并使用所选文本更新文本视图。这完美地工作。

但是,我的问题是:当用户执行此操作并更新 textview 时,如果他们按一次后退按钮,它将带他们到 Activity B,但如果按两次,则会在更新 TextView 之前将他们带到 Activity A,从而显示旧的文本视图,尽管已将更新的值存储在 SharedPreferences 中。如何解决这个问题?

我的问题的一个更简化的版本是,我的布局中有一个 TextView,以及一个按钮,如果按下该按钮,则会将其删除并刷新 Activity。用户按下删除按钮,文本视图消失,但随后按下返回按钮,TextView 被恢复。那是我不想要的。

我研究了所有的后退按钮方法和 savedInstanceState 文档,但我仍然没有找到有效的方法。我还尝试在我的操作栏中添加一个 UpNavigation 按钮,但它的效果与后退按钮相同。

活动 A(所有这些代码都在 OnCreate 中调用)

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    String sound1name = prefs.getString("sound1", "1");

TextView sound1TV = findViewById(R.id.sound1);
    sound1TV.setText(sound1Name);

 ImageView sound1btn = findViewById(R.id.sound1_btn);
    sound1btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent1 = new Intent(getApplicationContext(), SoundSelector.class);

            startActivity(intent1);
        }
    });

活动 B(调用适配器)

AudioFileAdapter aFilesAdapter = new AudioFileAdapter(SoundSelector.this, audioFileList, soundID);
    ListView listView = findViewById(R.id.sounds_list);
    listView.setAdapter(aFilesAdapter);

活动 B 中的适配器(选择文本时的 OnClickListener)

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(contextL);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("sound1", sound1string);
                    editor.apply();
                    Intent intent1 = new Intent(getContext(), SoundEditor.class);
                    con.startActivity(intent1);

我不确定这是我必须修改的活动生命周期,还是意图,或其他东西,但如果有人能指出我正确的方向,我将非常感激,如果您需要更多信息或代码,我会尽快发布尽可能。

标签: javaandroid

解决方案


方法Activity BActivity A通过重新启动前面的活动而不是导航来onBackPressed()导航。此外,如果导航是更新值的重要组成部分,那么推荐的string方法是使用startActivityForResult()和更新首选项和TextViewonActivityResult()Activity A

class ActivityA extends AppCompatActivity {
     private static final int activityRequest = 0x22;
     public static final String keyPref = "keyToSharedPrefData";
     private TextView mTextView;
     private boolean isHidden = false;


     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.a_activity);

         mTextView = findViewById(R.id.someTextView);
         final String textForTextView = SharedPrefUtils.getString(keyPref);
         mTextView.setText(textForTextView);

         final Button button = findViewById(R.id.someButton);

         if (button != null) {
             button.setOnClickListener((view) -> {
                 final Intent intent = new Intent(ActivityA.this, AcitivtyB.class);
                 startActivityForResult(intent, activityRequest);
             });
         }             

         final deleteButton = findViewById(R.id.delete_button);
         if (deleteButton != null) {
             deleteButton.setOnClickListener((view) -> {
                  mTextView.setText("");
                  isHidden = true;
             });
         }
     }

     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data){
         if (requestCode == activityRequest && resultCode == ActivityB.resultSuccess && data != null) {
             if (data.containsKey(ActivityB.resultKey)) {
                 SharedPrefUtils.saveString(keyPref,
                     data.getString(ActivityB.resultKey, SharedPrefUtils.getString(keyPref));

                 if (mTextView != null) {
                     mTextView.setText(SharedPrefUtils.getString(keyPref));
                 }
             }
         }
         if (isHidden) {
             mTextView.setVisibility(View.GONE); 
         } 
     }

}

ActivityB你可以

class ActivityB extends AppCompatActivity {
    public static final int resultSuccess = 0x11
    public static final int resultFailure = 0x33
    public static final String resultKey = "keyForResult"         

    private void onListItemClick(final String soundString) {
        // optional you can also do this
        SharedPrefUtils.saveString(ActivityA.keyPref, soundString);

        // better to do this
        final Intent returnIntent = getIntent() != null ? getIntent() : new Intent();

        returnIntent.putExtra(resultKey, soundString);
        if (getCallingActivity() != null) {
            setResult(returnIntent, resultSuccess);
        }
        onBackPressed();
    }
}

推荐阅读