Android startActivity() 回调后 onCreate() 数据丢失
Android data is lost onCreate() after startActivity() for callback
我有一个 android 应用程序,它通过 twitter4j 连接到 Twitter。在 activity 中有一个共享按钮和一个 TextView。流程如下;
- 用户打开 activity 开始填充
文本视图
- 用户点击"Share"
- Twitter activity 已针对 oAuth、用户类型帐户启动
凭据并授予我的应用程序权限
- 用户再次从 onCreate()
重定向到 activity
我的问题是 TextView 的值在第二次 onCreate() 后丢失。
- onCreate()
.
.
point = getIntent().getExtras().getInt("point");
.
.
- shareButton.onClick()
.
.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(requestToken.getAuthenticationURL()));// open twitter for authentication
intent .putExtra("point", point);
startActivityForResult(intent , 1);
}
});
thread.start();
.
.
到目前为止我已经尝试过的事情;
- 使用 startActivityForResult() 而不是 startActivity()
- 在 startActivity() 之前使用 intent.putExtra() 并在 onCreate() 中调用 getIntent().getExtras().getInt("point") 但没有此类数据
- 使用 onSaveInstanceState() 和 onRestoreInstanceState() 但 savedInstanceState 为空
请告诉我如何在 Twitter 回调后保留此数据。我可以使用数据库或共享首选项或类似的东西,但还有其他方法吗?
提前致谢。
编辑:
当程序转到推特并返回时,它会再次从 onCreate() 开始,就像方向改变时一样。当方向改变时 "Bundle savedInstanceState" 有价值,但在我的例子中没有价值。
如何在 twitter 回调的 return 之后的 onCreate() 的第二个 运行 中获取我之前手动设置的 savedInstanceState?
编辑 2:
本站有些人真的很奇怪。我 post 将下面的答案编辑为新的 post 但有人删除了它声称 "the new post should include the answer"。你知道吗,post 有真正的答案。不要在没有阅读所有内容的情况下这样做。我将其作为编辑写在下面。
非常感谢您的回答 (@rod_torres)。我已经尝试过您的解决方案(使用和不使用 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)),但在这两种情况下我都无法实现。我设置singleTop时的生命周期如下;
onCreate() -> Twitter -> onCreate()
从 Twitter 返回后未调用 onNewIntent()。 activity 已重新创建为新实例,因此我可以按上面的顺序使用后退按钮返回。
我尝试使用 singleTask 并且成功了(我的问题的答案是这个,对于那些没有阅读这部分就删除了我的 post 的人)。它没有创建 activity 的新实例,并且调用了 onNewIntent() 。当我单击后退按钮时,我被重定向到我应用程序的前一个 activity,而不是浏览器(Twitter)的任务,尽管 Twitter 是前一个 activity。流程就像;
onCreate() -> Twitter -> onCreate(with bundles) -> onNewIntent()
Activity A -> Activity B -> Twitter -> Activity B(同上)
当我从Activity B回来的时候;
Activity A -> 浏览器
似乎 singleTask 模式将我的应用程序的任务带到了前台,其中包含所有活动,并将其他应用程序(浏览器)的任务带到了后台堆栈。我对吗?
但我仍然无法理解为什么您的解决方案对我不起作用。我已经阅读了很多关于此的文档,我认为它应该已经起作用了。
此外,我不确定是否应该使用 singleTask。会不会有什么问题我还没有考虑到,有什么缺点吗?
正如您在编辑中指出的那样,Activity 已重新创建。要使应用程序不重新创建 Activity,请在清单中将值为 "singleTop" 的参数 launchMode 添加到您将使用 twitter4j
的 Activity
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second"
android:launchMode="singleTop">
此外,当您调用 activity 时添加标志 FLAG_ACTIVITY_CLEAR_TOP:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("string","Second Activity Biches");
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
更多信息:
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
我有一个 android 应用程序,它通过 twitter4j 连接到 Twitter。在 activity 中有一个共享按钮和一个 TextView。流程如下;
- 用户打开 activity 开始填充 文本视图
- 用户点击"Share"
- Twitter activity 已针对 oAuth、用户类型帐户启动 凭据并授予我的应用程序权限
- 用户再次从 onCreate() 重定向到 activity
我的问题是 TextView 的值在第二次 onCreate() 后丢失。
- onCreate()
.
.
point = getIntent().getExtras().getInt("point");
.
.
- shareButton.onClick()
.
.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(requestToken.getAuthenticationURL()));// open twitter for authentication
intent .putExtra("point", point);
startActivityForResult(intent , 1);
}
});
thread.start();
.
.
到目前为止我已经尝试过的事情;
- 使用 startActivityForResult() 而不是 startActivity()
- 在 startActivity() 之前使用 intent.putExtra() 并在 onCreate() 中调用 getIntent().getExtras().getInt("point") 但没有此类数据
- 使用 onSaveInstanceState() 和 onRestoreInstanceState() 但 savedInstanceState 为空
请告诉我如何在 Twitter 回调后保留此数据。我可以使用数据库或共享首选项或类似的东西,但还有其他方法吗?
提前致谢。
编辑:
当程序转到推特并返回时,它会再次从 onCreate() 开始,就像方向改变时一样。当方向改变时 "Bundle savedInstanceState" 有价值,但在我的例子中没有价值。
如何在 twitter 回调的 return 之后的 onCreate() 的第二个 运行 中获取我之前手动设置的 savedInstanceState?
编辑 2:
本站有些人真的很奇怪。我 post 将下面的答案编辑为新的 post 但有人删除了它声称 "the new post should include the answer"。你知道吗,post 有真正的答案。不要在没有阅读所有内容的情况下这样做。我将其作为编辑写在下面。
非常感谢您的回答 (@rod_torres)。我已经尝试过您的解决方案(使用和不使用 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)),但在这两种情况下我都无法实现。我设置singleTop时的生命周期如下;
onCreate() -> Twitter -> onCreate()
从 Twitter 返回后未调用 onNewIntent()。 activity 已重新创建为新实例,因此我可以按上面的顺序使用后退按钮返回。
我尝试使用 singleTask 并且成功了(我的问题的答案是这个,对于那些没有阅读这部分就删除了我的 post 的人)。它没有创建 activity 的新实例,并且调用了 onNewIntent() 。当我单击后退按钮时,我被重定向到我应用程序的前一个 activity,而不是浏览器(Twitter)的任务,尽管 Twitter 是前一个 activity。流程就像;
onCreate() -> Twitter -> onCreate(with bundles) -> onNewIntent()
Activity A -> Activity B -> Twitter -> Activity B(同上)
当我从Activity B回来的时候;
Activity A -> 浏览器
似乎 singleTask 模式将我的应用程序的任务带到了前台,其中包含所有活动,并将其他应用程序(浏览器)的任务带到了后台堆栈。我对吗?
但我仍然无法理解为什么您的解决方案对我不起作用。我已经阅读了很多关于此的文档,我认为它应该已经起作用了。
此外,我不确定是否应该使用 singleTask。会不会有什么问题我还没有考虑到,有什么缺点吗?
正如您在编辑中指出的那样,Activity 已重新创建。要使应用程序不重新创建 Activity,请在清单中将值为 "singleTop" 的参数 launchMode 添加到您将使用 twitter4j
的 Activity<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second"
android:launchMode="singleTop">
此外,当您调用 activity 时添加标志 FLAG_ACTIVITY_CLEAR_TOP:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("string","Second Activity Biches");
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
更多信息:
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP