ActionBarSherlock重新申请的主题引发错误的生命周期方法生命周期、错误、方法、主题

由网友(深望眸底那抹情)分享简介:我用 SherlockFragmentActivity 有3个标签。每个选项卡都包含 SherlockFragment 。I use a SherlockFragmentActivity with 3 tabs. Each of these tabs are containing a SherlockFragment...

我用 SherlockFragmentActivity 有3个标签。每个选项卡都包含 SherlockFragment

I use a SherlockFragmentActivity with 3 tabs. Each of these tabs are containing a SherlockFragment.

如果我重新启动我的应用程序(应用主题)本code:(感谢但丁! )

If I restart my app (to apply a theme) with this code: (thanks to Dante!)

finish();
intent = new Intent(this, <your_activity>.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

应用程序shutsdown正常,但如果应用程序被再次启动,我的片段的整个生命周期方法被调用。一切工作正常,但有必要布尔设置为false 的onDestroy ,但片段的arent被破坏。手段,布尔是错误的。

the app shutsdown properly but if the app are starting again, the whole lifecycle methods of my fragments are get called. Everything works fine, but one needed boolean are set to false in onDestroy but the fragment arent destroyed. Means, the boolean is wrong.

有没有办法解决?我做错什么了吗?

Is there a way to fix? Did I do something wrong?

推荐答案

您必须保存价值的地方,例如在数据库或在共享preferences。

You have to save the value somewhere,for example in the database or in the shared preferences.

在你申请的 setTheme(主题); 你要检索值

这是初始值:

public static int THEME = R.style.Theme_Sherlock;

首先设定值(例如黑暗),并重新启动应用程序:

First set the value (for example "dark") and restart the app:

DBAdapter db = new DBAdapter(this);
try {
    db.open();
    db.UpdateOption("theme", "dark");
}
catch (Exception ex) {}
finally {
    db.close();
}
finish();
Intent intent = new Intent(this, ActionBarTabsPager.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

然后你获得新的价值,并设立了主题

Then you get the new value and set the THEME

DBAdapter db = new DBAdapter(this);
Cursor c = null;

try {
    db.open();
    c = db.GetOption(c, "theme");
    String theme = c.getString(1);
    if (theme.equalsIgnoreCase("dark")) {
        THEME = R.style.Theme_Sherlock;
    }
    else if (theme.equalsIgnoreCase("light")) {
        THEME = R.style.Theme_Sherlock_Light;
    }
    else if (theme.equalsIgnoreCase("darklight")) {
        THEME = R.style.Theme_Sherlock_Light_DarkActionBar;
    }
}
catch (Exception ex) {}
finally {
    try {
            if (c != null)
            {
                c.close();
                c = null;
            }
        }
        catch (Exception ex){}
        db.close();
}
setTheme(THEME);

我有一个TABEL选项保存一些设置。这也可以用共享$ P $当然pferences进行。

I have a tabel OPTIONS to save some settings. This can also be done with shared preferences of course.

阅读全文

相关推荐

最新文章