开发者

Android: Reusing an activity stored as local variable in another activity

I have two activities in my Android 2.1 application.

The first is the Main activity, with a view offering touch interaction.

The second is the Settings activity, offering settings to adjust parameters which are used within Main.

I currently have a Settings activity within my Main class, as a class instance. I read settings from this instance within Main.

public class Main extends Activity implements View.OnClickListener, View.OnTouchListener {
    protected Settings settings;
}

public class Settings extends Activity implements ListAdapter {
}

I've discovered how to reuse my Settings activity if it has already been created, ensuring only one persistent instance:

// within Main.java :
Intent intent = new Intent(this开发者_如何学Python,Settings.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); // reuse existing Settings
this.startActivity(intent);

My issue is that I cannot discover how to tie this in with the local 'settings' member in Main.

Would someone be kind enough to give me a quick example of how I can start my local 'settings' instance as a reusable activity?

Many thanks


I currently have a Settings activity within my Main class, as a class instance.

Delete this immediately. One activity should not be holding onto another activity, as this results in memory leaks. I guarantee you that there are better solutions for whatever problem you think that you are solving this way.

My issue is that I cannot discover how to tie this in with the local 'settings' member in Main.

By deleting "the local 'settings' member in Main", this problem goes away.

Also, please use SharedPreferences and a PreferenceActivity for collecting "settings" wherever possible. For example, if the point behind "the local 'settings' member in Main" is to allow Main to access the settings, the right answer for that is for the settings to be stored in a SharedPreferences object and for Main to be using those SharedPreferences. Using a PreferenceActivity has the benefit of giving the user a look and feel that they expect when providing settings to an application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜