如何prevent活动摧毁在Android应用程序? [英] How to prevent activity from destroying in android app?

查看:102
本文介绍了如何prevent活动摧毁在Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下次使用code。使用现有客户共享一些文本:

I use next code to share some text using available clients:

    Intent share = new Intent(Intent.ACTION_SEND);
    share.putExtra(Intent.EXTRA_TEXT, "Here's some text for Twitter.");
    share.setType("text/plain");
    startActivity(Intent.createChooser(share, "Share this via"));       

它的工作原理(打开Twitter客户端,如样品),但是当我关闭该应用程序,我发现我的应用程序也将关闭。但我需要我的应用程序仍然活跃。我需要做的呢?

It works (opens twitter client, as sample), but when I was close that app, I was find that my app also closed. But I need my app still to be active. What I need to do for this?

推荐答案

您的应用程序会在后台运行,onResume和在onPause都将被称为,前提是操作系统有足够的内存,以保持新的应用程序,所有的旧的应用程序。

Your app will be in the background, onResume and onPause will both be called, provided that the OS have enough memory to keep the new app and all the old apps.

  • 如果您有需要,而用户不看它,使用的服务的长期运行的进程。

  • If you have a long running process that you need while the user not looking at it, use a service.

如果您需要用户返回应用程序在相同的状态,的你需要当用户回来做onResume和的onPause工作,保存状态信息,并初始化UI一次。如果你真的很担心,它可以被杀死(当然,这不应该失去的包,我认为),你可以分享preferences它们存储你的应用程序。

If you need the user to return the app in the same state, you need to do the work in onResume and onPause to save the state information and initialize your UI again when the user comes back. If you are really worried that it can get killed (well, it shouldn't lose the bundle I think), you can store them in SharePreferences for your app.

如果你想知道什么时候从特定共享意图应用程序的回报,使用startActivityForResult

If you want to know when the app returns from that specific share intent, use startActivityForResult

这篇关于如何prevent活动摧毁在Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆