android - android中同一应用程序的多个实例

标签 android

如何在从图库应用程序内部共享图像时使用已经运行的应用程序? 它总是在创建已经运行的应用程序的单独实例。 我在 whatsapp 应用程序中观察到同样的问题。

最佳答案

是的。另一个用例是当您点击通知时。如果应用程序已经在后台,它将启动一个新实例。

使用 android:launchMode

<activity android:launchMode = ["standard" | "singleTop" | "singleTask" | "singleInstance"] ../>

所以使用 "singleTop"

来自 the docs :

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).

请阅读下面的博文

http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

https://www.mobomo.com/2011/06/android-understanding-activity-launchmode/

已编辑:在 OnResume 之前,它会调用 OnActivityResult。使用已选择的数据

Intent intent = new Intent();

    intent.setType("image/jpeg");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(intent,
                    "Select Image From Gallery"),
                    10000);


    @Override
        protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
Uri originalUri = null;
        if (resultCode == Activity.RESULT_OK) {
    if (requestCode == 10000) {
    
                    if (data.getData() != null) {
                        originalUri = data.getData();
                        //originaluri is your selected image path.
            
    
                    }}}

并在我们的应用中获取 Intent Filter Action。

void onCreate (Bundle savedInstanceState) {
    ...
    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
        } else if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    } 

http://developer.android.com/training/sharing/receive.html http://developer.android.com/training/basics/intents/filters.html

或者如果您使用的是启动模式-singleTop。只需覆盖 onNewIntent

 @Override
    protected void onNewIntent(Intent intent) {


        super.onNewIntent(intent);
       
    }

关于android - android中同一应用程序的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35506704/

相关文章:

javascript - 在 Chrome 上使用 Javascript 捕获 Android 中的 native 后退按钮

android - 如何取消闹钟?

c# - 适用于 Android 的 Datepicker DialogFragment 单声道

android - 无法在手机上安装 .apk

android - 是否可以在 Android 上使用 OpenCV 进行缩放和聚焦?

android - 使用对象作为参数从 Java 调用 RESTful WCF 服务方法

android - 当我尝试调试时应用程序崩溃

android - 模拟器没有启动并给出一些错误

android - 选项卡主机 - setCurrentTab(x) 适用于选项卡但不适用于内容 [Android]

java - 在 Android java 中单击导航项时如何启动新 Activity