【发布时间】:2013-08-27 21:02:03
【问题描述】:
根据 Google 指南,建议您仅在安装并打开应用后第一次打开 DrawerLayout(以向用户展示功能)。
你会怎么做呢?
这似乎是openDrawer() 方法与某种偏好的组合。
【问题讨论】:
标签: android design-patterns navigation-drawer drawerlayout
根据 Google 指南,建议您仅在安装并打开应用后第一次打开 DrawerLayout(以向用户展示功能)。
你会怎么做呢?
这似乎是openDrawer() 方法与某种偏好的组合。
【问题讨论】:
标签: android design-patterns navigation-drawer drawerlayout
我建议您为此使用 SharedPreferences:
基本思路是您阅读 SharedPreferences 并查找在第一次应用启动时不存在的布尔值。 默认情况下,如果您要查找的值,您将返回“true” 找不到,说明其实是第一个app启动。然后,在您的第一个应用程序启动后,您将存储该值 “false”在您的 SharedPreferences 中,并且在下次启动时,该值 “false”将从 SharedPreferences 中读取,表示它是 不再是第一个应用启动。
以下是它的外观示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
// your other code...
// setContentView(...) initialize drawer and stuff like that...
// use thread for performance
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SharedPreferences sp = Context.getSharedPreferences("yoursharedprefs", 0);
boolean isFirstStart = sp.getBoolean("key", true);
// we will not get a value at first start, so true will be returned
// if it was the first app start
if(isFirstStart) {
mDrawerLayout.openDrawer(mDrawerList);
Editor e = sp.edit();
// we save the value "false", indicating that it is no longer the first appstart
e.putBoolean("key", false);
e.commit();
}
}
});
t.start();
}
【讨论】: