如何在android中强制更新主要活动?

时间:2015-12-07 18:13:16

标签: java android xml android-activity android-settings

这个问题是对this未回答的问题的一种跟进,有更多更好的细节。

基本上,我有一个android主要活动,显示从数据库中获取的一些数据(通过SQLiteDatabase)。现在,当用户想要更改某些首选项或删除数据库的内容时,会显示PreferenceActivity。在后一种情况下,数据库的内容将被删除 - 但如何使主要活动更新?如果我在按下按钮删除数据库内容后查看主要活动,我希望显示主要活动零条目!

以下是主要活动的相关部分:

public class MainActivity extends AppCompatActivity  {

    SharedPreferences.OnSharedPreferenceChangeListener mPrefListener;
    private SharedPreferences settings;
    public interface MyCallBack
    {
        public void refreshMainActivity();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, AlarmService.class);
        startService(intent);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mCallback = new MyCallBack() {
            @Override
            public void refreshMainActivity() {
                Toast.makeText(MainActivity.this, "TOAST MAINACTIVITY", Toast.LENGTH_LONG).show();
                MainActivity.this.recreate();
            }
        };
        /*
        settings = PreferenceManager.getDefaultSharedPreferences(this);
        mPrefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                Log.d("TEST", "testtest");

                Toast.makeText(MainActivity.this, "TOAST MAINACTIVITY", Toast.LENGTH_LONG).show();

                finish();
                startActivity(getIntent());
            }
        };
        */

        // Register the listener on the SharedPreferences
        settings.registerOnSharedPreferenceChangeListener(mPrefListener);

以下是UserSettingActivity.java的内容:

public class UserSettingActivity extends PreferenceActivity {

    private Preference myPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        myPreference = findPreference("reset");
        myPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference arg0) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(UserSettingActivity.this);
                alertDialog.setMessage("Are you sure to delete the database?");
                alertDialog.setCancelable(true);
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        final DBAdapter db = new DBAdapter(UserSettingActivity.this);
                        db.open();
                        db.resetDatabase();
                        callBack = MainActivity.mCallback;
                        callBack.refreshMainActivity();
                    } });
                alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    } });
                alertDialog.show();
                return false;
            }
        });

    }

}

最后,xml文件描述xml/preferences.xml中的设置布局:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference android:title="Your Name"
        android:key="username"
        android:summary="Please provide your username"></EditTextPreference>
    <CheckBoxPreference android:title="Application Updates"
        android:defaultValue="false"
        android:summary="This option if selected will allow the application to check for latest versions."
        android:key="applicationUpdates" />

    <Preference
        android:key="reset"
        android:title="Reset database"
        android:summary="This will remove every entry in the database"
         />
</PreferenceScreen>

当我点击“设置”屏幕上的按钮reset并确认此选项时,数据库将被删除resetDatabase执行两个表中的DROPCREATE TABLE在数据库中)。但是,Toast'TOAST MAINACTIVITY'永远不会显示在显示屏上!

我希望在这里能够做到这一点,因为我希望它有效吗?如何从UserSettingActivity

触发主要活动中的方法/功能/某些内容

1 个答案:

答案 0 :(得分:3)

使用回调解决方案可能有点棘手,但我希望根据您的条件这对您有用..

首先在MainActivity.java中创建接口,如下所示

public interface MyCallBack
{
 public void refreshMainActivity();
}

然后在您的主要活动中实现此界面,如此

public class MainActivity extends AppCompatActivity {
public static MyCallBack mCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

mCallback = new MyCallBack() {
    @Override
    public void refreshMainActivity() {
           MainActivity.this.recreate();

              "OR"

              finish();
              startActivity(getIntent());
         }
    };
}

现在在您的userSettingActivity.java中使用此接口,并在您想要更新MainActivity.java时调用其方法

public class UserSettingActivity extends AppCompatActivity{
MainActivity.MyCallBack callBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settingsactivity);
    callBack = MainActivity.mCallback;

    //this is sample code ..call this function when you want to update mainactivity
    callBack.refreshMainActivity();
}

}