重新创建一个活动并传递参数

8

我有一个监听偏好设置更改并重新加载应用程序的活动。我使用recreate()来实现这一点。但是我不知道如何通过它传递参数,因此我已经转而手动重新加载活动。

Intent intent = getIntent();
finish();
// add in the arguments as Extras to the intent
startActivity(intent);

这个方法的行为符合我的期望,但是重新创建活动对用户来说并不流畅,因为他们会看到活动被杀死,然后再次启动相同的活动。我希望用户不知道活动已经被重新启动。所以,我的问题是,我能否使用 recreate() 方法并仍然通过它传递参数。


你可以使用recreate()函数,并将参数保存在sharedPreference中。 - 7geeky
这是唯一可能的方式吗? - fuadj
请看一下这个答案。 - Soon Santos
2个回答

9

在调用重新创建之前,您可以在活动意图中设置数据。

        getIntent().putExtra("RECREATE_DATA", "Some Data");
        recreate()

由于重建活动时将使用相同的活动实例,因此在重建后,意图中的数据仍然存在。


出色的解决方案,我也可以在抽象类中使用。 - Noor Hossain
这个答案对我不起作用。 - Thanvandh

3
你可以尝试这种方法:你可以将启动模式设置为SingleTop并处理onNewIntent(Intent intent)方法来重新启动活动。这样,你就可以重新启动活动并发送意图,同时不会杀死该活动,即你的活动不会被创建。
public class MainActivity extends Activity implements View.OnClickListener {
    Button btn ;
    String mRelaunchData ;
    public static String TAG = "RelaunchMainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(this);
        Log.e(TAG, "onCreate called");
    }

    @Override
    public void onClick(View view) {
        Log.e(TAG, "onClick called");
        Intent intent = new Intent("relaunch.activity.ACTIVITY_SELF_START_INTENT").setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("RESTART_DATA", "This is relaunch of this Activity");
        startActivity(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e(TAG, "onNewIntent called");
        mRelaunchData = intent.getStringExtra("RESTART_DATA");
        Log.e(TAG, "mRelaunchData =" + mRelaunchData);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume called");
        if(mRelaunchData != null){
            Toast.makeText(MainActivity.this, mRelaunchData, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause called");

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart called");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy called");
    }
}

在 AndroidManifest.xml 文件中。
 <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="relaunch.activity.ACTIVITY_SELF_START_INTENT" />
                <category android:name = "android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

onClick将重新启动Activity。

生命周期将是:

-onclick

-onPause

-onNewIntent

-onResume


但是我希望调用oncreate方法,问题的重点是处理首选项更改(这会显着更改整个UI),我在oncreate中进行UI操作,因此需要调用它。 - fuadj
一种方法是在onCreate中创建所有视图,并将不需要的UI集合的setvisibility设置为false,然后一旦你获得了onNewIntent,就使用意图并将这些视图集合的可见性设置为true,另一个集合则设置为false。这样用户就不会意识到活动已经重新启动了。 - Raman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接