2

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

enter image description here

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?

Zahid H
  • 235
  • 5
  • 18
  • Remove addFlags. Using default is enough. If no more reasons to use this flag, don't use :) – TeeTracker Aug 15 '13 at 21:12
  • @TeeTracker I removed the flag. The issue now is, If I click on the image and it asks me to choose an action and I, instead press BACK, my app just closes (does not FC) but I see a lot of error on my LogCat. – Zahid H Aug 15 '13 at 21:15
  • My error log is here: http://pastebin.com/idcAYEje – Zahid H Aug 15 '13 at 21:18
  • 1
    Hmmm...doesn't sound right. You shouldn't be experiencing this behavior. Post more code to tell the larger story – MobileMon Aug 15 '13 at 21:19
  • I added `dialog.dismiss();` before the `startActivity(intent)` on all of those three options. Now I do not receive the error but when the `Complete action using` window comes up and instead of picking an option I press `Back` button my app just closes. Any idea how to fix it? – Zahid H Aug 15 '13 at 21:26
  • Some more code surrounding this snippet would be useful. Perhaps you have a handle on the Dialog that is leaking.. – MiStr Aug 15 '13 at 21:28
  • Do you have a "finish()" anywhere in your code? – MiStr Aug 15 '13 at 21:29
  • http://pastebin.com/5cD3SDur < this is the link to the full code. – Zahid H Aug 15 '13 at 21:30
  • I have onStop() onPause() onResume onDestroy() onRestart() but no finish() – Zahid H Aug 15 '13 at 21:31
  • Include update to log file (when back is pushed) – MiStr Aug 15 '13 at 21:48
  • http://pastebin.com/ut4Z84uA (log file when back is pressed) – Zahid H Aug 15 '13 at 21:49
  • My MainActivity has three tab fragments. I have `noHistory=true` on the AndroidManifest for the MainActivity. Maybe because the About Dialog is called from the MainActivity hence once the new activity starts it just closes? – Zahid H Aug 16 '13 at 14:19

2 Answers2

2

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")

Community
  • 1
  • 1
MiStr
  • 1,193
  • 10
  • 17
  • I added dialog.dismiss(); before the startActivity(intent) on all of those three options. Now I do not receive the error but when the Complete action using window comes up and instead of picking an option I press Back button my app just closes. Any idea how to fix it? – Zahid H Aug 15 '13 at 21:28
  • try adding `intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);` ? – MiStr Aug 15 '13 at 21:35
  • I added that code above `startActivity(intent)` but did not work. – Zahid H Aug 15 '13 at 21:38
  • Same thing, when I press Back it just goes back home rather than going back to my app. – Zahid H Aug 15 '13 at 21:39
  • Can't tell from those logs. I'd need to compile this code, and see what the debugger shows.. sorry! – MiStr Aug 15 '13 at 21:52
  • What else you think end from me? – Zahid H Aug 15 '13 at 21:57
  • Not sure yet. I may be able to test more tomorrow. – MiStr Aug 16 '13 at 00:23
  • My MainActivity has three tab fragments. I have `noHistory=true` on the AndroidManifest for the MainActivity. Maybe because the About Dialog is called from the MainActivity hence once the new activity starts it just closes? – Zahid H Aug 16 '13 at 14:20
  • I modified the code for all the onACTION() methods. It seems when I click the link and the "complete action using" pops up, onPause() is called and pressing the `Back` button calls the onStop() and onDestroy() method. So somehow my app is just closing. – Zahid H Aug 16 '13 at 18:01
  • I was reading thru: http://developer.android.com/training/basics/activity-lifecycle/pausing.html – Zahid H Aug 16 '13 at 18:03
  • Fixed it... I had `noHistory="true"` which was causing this issue. – Zahid H Aug 16 '13 at 21:06
1

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?

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • My MainActivity has three tab fragments. I have `noHistory=true` on the AndroidManifest for the MainActivity. Maybe because the About Dialog is called from the MainActivity hence once the new activity starts it just closes? – Zahid H Aug 16 '13 at 14:19
  • try to remove the flag of "noHistory" and see what happens. instead of this flag , choose exactly how and when you wish to close the activity. – android developer Aug 17 '13 at 15:57