简体   繁体   English

在不丢失父活动的情况下打开新活动

[英]Open new activity without losing parent activity

I have a DetailsActivity that lists several thumbnails of images.我有一个DetailsActivity列出了几个图像缩略图。 I want to open a new activity, by passing in the URL as a string, to a " FullScreenActivity " that allows the user to see the larger version of the image.我想通过将 URL 作为字符串传递给“ FullScreenActivity ”来打开一个新活动,该活动允许用户查看较大版本的图像。

The DetailsActivity contains a complex data object that I don't want to loose when the user opens the full screen view. DetailsActivity包含一个复杂的数据对象,当用户打开全屏视图时,我不想丢失它。

I can pass the string to the new " FullScreenActivity ", the image downloads, and I can pinch/zoom the images etc.. but when I click the UP button to return to the DetailsAcitivity , the app crashes because the complex data object is gone.我可以将字符串传递给新的“ FullScreenActivity ”,图像下载,我可以捏/缩放图像等。但是当我单击向上按钮返回到DetailsAcitivity 时,应用程序崩溃,因为复杂的数据对象消失了.

How do I keep the DetailsActivity around while the user is looking at a bigger image?当用户查看更大的图像时,如何保持DetailsActivity

Here is how I call the FullScreenActivity.这是我调用 FullScreenActivity 的方式。 This is called in a for loop that creates a new image view and add it to the activity.这在创建新图像视图并将其添加到活动的 for 循环中调用。

image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(DetailsActivity.this, ViewFullPhotoActivity.class);
intent.putExtra("FullScreenPicture", (String) finalImageMap.get("MainImage"));
startActivity(intent);
}
});

And in my FullScreenActivity , I get the URL string via在我的FullScreenActivity 中,我通过

String fullScreenImage = (String) getIntent().getCharSequenceExtra("FullScreenPicture");

I toyed with the idea of packaging up the complex data object and sending it along with the new intent, but surely that isn't a best practice.我想过打包复杂数据对象并将其与新意图一起发送的想法,但这肯定不是最佳实践。 There are other things that would not be great for the user if I went that path, specifically re downloading all the thumbnails, or loosing data entered into a form.如果我走这条路,还有其他一些对用户不利的事情,特别是重新下载所有缩略图,或者丢失输入到表单中的数据。

I would suggest to override onSaveInstanceState() , save your data there and inside onCreate() get your saved data from savedInstanceState .我建议覆盖onSaveInstanceState() ,将您的数据保存在那里,并在onCreate()内部从savedInstanceState获取您保存的数据。 See the documentation for more.有关更多信息,请参阅文档

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // Save your data here with savedInstanceState.put<something>...
}

If your data is complex (not a string or int or some simple type), you can write a method for saving it to a Bundle (what can be put in the savedInstanceState bundle), and another one which parses a bundle and restores your data from it).如果您的数据很复杂(不是字符串或 int 或一些简单类型),您可以编写一个方法将其保存到一个Bundle (可以放在savedInstanceState包中的内容),以及另一个解析包并恢复您的数据的方法从中)。 But it is not a good idea storing images like this... you could cache them on sdcard (eg in the data directory of your app, but not in the memory).但是像这样存储图像并不是一个好主意......你可以将它们缓存在sdcard上(例如在你的应用程序的数据目录中,而不是在内存中)。


Another option to show the contents of your " FullScreenActivity " in a full screen dialog instead an activity, so the DetailsActivity won't be destroyed in the background.在全屏对话框而不是活动中显示“ FullScreenActivity ”的内容的另一种选择,因此不会在后台销毁DetailsActivity

If your complex data structure cannot be saved using onSaveInstanceState , then the easiest method is to save it in a Singleton object and then keep the complex object in memory.如果您的复杂数据结构无法使用onSaveInstanceState保存,那么最简单的方法是将其保存在 Singleton 对象中,然后将复杂对象保存在内存中。 You can find an example here .您可以在此处找到示例。

However, it's not the best approach.然而,这不是最好的方法。 You might encounter unexpected NULL pointer exceptions if you assume that the complex object will be in memory all the time.如果您假设复杂对象将一直在内存中,您可能会遇到意外的 NULL 指针异常。 If you want to go further, then you can either如果你想更进一步,那么你可以

  • Serialize the object and save it to disk序列化对象并将其保存到磁盘
  • Use a SQLite database and save it in the DB使用 SQLite 数据库并将其保存在 DB 中

I believe the latter 2 methods are better, but require some significant work depending on how complex your data structure is.我相信后两种方法更好,但需要一些重要的工作,具体取决于您的数据结构的复杂程度。

This will do.这会做。 Inside Your Manifest file.在您的清单文件中。

 <activity
            android:name=".FullScreenActivity"
            android:label="FullScreenActivity"
            android:launchMode="singleTop"
            android:parentActivityName=".DetailsActivity">

This will specify your fullscreen Activity will be the child of DetailsActivity.这将指定您的全屏 Activity 将是 DetailsActivity 的子项。 so this will open the Fullscreen activity on Top of DetailsActivity.所以这将打开细节活动顶部的全屏活动。

I found a much simpler solution.我找到了一个更简单的解决方案。 In my android manifest, in my DetailsActivity I simply added android:launchMode="singleTop" .在我的 android manifest 中,在我的DetailsActivity 中,我只是添加了android:launchMode="singleTop" That did the trick.这就是诀窍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM