简体   繁体   中英

Running app in the background when going to a new activity

I have the following code which opens up a browser to my social profile:

    ImageView iv1 = (ImageView) dialog.findViewById(R.id.imgFB);
    iv1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Uri uri = Uri.parse("https://www.facebook.com/profile");                
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);  
            }
    });

Now when I choose the browser to open the profile, and press back , it not only closes the browser but also my app. Any way to just close the browser while keeping my app running in the background?

UPDATE :

This works fine:

ImageView iv1 = (ImageView) dialog.findViewById(R.id.imgFB);
iv1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Uri uri = Uri.parse("https://www.facebook.com/PagesByZ"); //Uri.parse("market://details?id=" + facebookpackagename); 
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        dialog.dismiss();
        startActivity(intent);  
    }
});

Now I do not receive the error but when the Complete action using window comes up and instead of picking an option I just press Back, my app just closes. Any idea how to fix it so If I press Back it goes back to my app rather than Home?

Update : The following is a rewording to help understand the issue better.

In my app I have a image link which opens the browser, but if the user has multiple browser installed it brings up the "Complete action using" window where I can choose. If I press the back button it should close it come back to my app. Instead what is happening is, if I press the back button my app just closes and I am taken back to homescreen

在此处输入图片说明

My MainActivity: http://pastebin.com/FVwEKLAT

My AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sikni8.tollculator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sikni8.tollculator.MainActivity"
            android:label="@string/app_name"
            android:noHistory="true"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.sikni8.tollculator.CurrentTrip"
            android:label="@string/title_activity_current_trip"
            android:parentActivityName="com.sikni8.tollculator.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.sikni8.tollculator.MainActivity" />
        </activity>
        <activity
            android:name="com.sikni8.tollculator.DisplayTrip"
            android:label="@string/title_activity_display_trip"
            android:parentActivityName="com.sikni8.tollculator" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.sikni8.tollculator" />
        </activity>
        <activity
            android:name="com.sikni8.tollculator.ShowHelp"
            android:label="@string/app_name" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.sikni8.tollculator.MainActivity" />
        </activity>
        <activity
            android:name="com.sikni8.tollculator.ShowSettingOptions"
            android:label="@string/app_name" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.sikni8.tollculator.MainActivity" />
        </activity>
    </application>

</manifest>

How can I make sure that If the user changes their mind on not going to the browser and presses the back button it just goes back to my app instead of going to the home screen?

Also would fixing the above fix, in case they do goto the browser and presses the back which will close the browser and my app will come back to view?

Perhaps a dialog is trying to be shown from an Activity (your MainActivity) that was also closed... Are you triggering a dialog after they click the ImageView?

See this post as well. (since your logs show "com.myapp.tollculator.MainActivity has leaked window")

some possible causes/solutions :

  1. i can't believe this is the cause, but is it possible your app is being closed because either your app or the web browser app use too much memory, or maybe you test on a low end device?

  2. also , please try a combination of the next flags: FLAG_ACTIVITY_NEW_TASK , FLAG_ACTIVITY_NO_HISTORY , FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET . please also try to

  3. please check what happens on another device. even the emulator.

  4. please show some log when you open the dialog and press on back.

  5. please go to the settings of the device, and see if the option (in the development section) "do not keep activities" is turned on or not.

  6. please create a totally new project with just the dialog you use, and try it out. maybe it's a problem outside of what you think...

  7. show the manifest part of the activity that you are using.

  8. sure there is no call for "finish()" anywhere in the code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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