Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

Openeing browser in android app

Hi there,

I'm creating an app which, when opened, launches a website in the browser, for which I'm using intent method.

However when the user clicks on the back button, it brings him back to the "Main activity" where "Hello World" is appearing as I've left the activity_main.xml

I want to achieve the functionality that when a user clicks on a back button, the browser should exit or rather minimize than going on to the main activity/home screen.

Following is the code.

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(browserIntent);

However I tried using the addFlags method but it didn't work.

What modifications should I do?

Regards, Akshay

12 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I don't have a good answer. I don't know if you can detect an Activity was resumed because of a user hitting the Back button from another app. You can override Back button functionality within your app, but that's not what you're looking for here.

One possibility is to set a flag in your Activity before you start the Intent for the browser. Then in your Activity's onResume() method, check to see if that flag is set. If so, exit your app using finish() or one of the solutions here: http://stackoverflow.com/q/3226495/475217

Ben Jakuben
Ben Jakuben
Treehouse Teacher

On second thought, perhaps this is what you're looking for: Intent.FLAG_ACTIVITY_TASK_ON_HOME

When I've used addFlags I've done it in the other order, i.e. NEW then CLEAR. I have no idea if that solves your problem, though.

I guess if you're opening a browser, rather than displaying the web page in the app, then clicking back would close the browser window and show your app that was running behind.

If you use a WebView rather than the browser you can control the order of views more easily with addFlags.

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

Well thanks for the advice Steve, but there are a few disadvantages of Webview like no browser-like widgets, does not enable JavaScript and web page errors are ignored. Thus I'm preferring to launch the website in separate browser. Hey Ben Jakuben , can you help us here?

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

Going by your second thought, could you briefly explain how to use the "Intent.FLAG_ACTIVITY_TASK_ON_HOME" with the above code? Sorry for the noob question.

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

I mean if you could explain how the code will be positioned completely along with this?

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Before you start it, set the flags for browserIntent just like you do in the example from the project using the setFlags() method. :) (Disclaimer: I haven't tried it myself to see if it gives you your desired behavior.)

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

Well do you mean this?

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(browserIntent);       
    }

    public void onResume()
    {
         finish();      
    }
Ben Jakuben
Ben Jakuben
Treehouse Teacher

If trying out a flag, I meant to try something like this:

public class MainActivity extends ActionBarActivity {

    private boolean mActivityShouldFinish = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        mActivityShouldFinish = true;

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(browserIntent);       
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (mActivityShouldFinish) {
             finish();
        }
    }
}
Akshay Shivpuri
Akshay Shivpuri
2,917 Points

Well there is some issue here,

Everytime I run it, I get an alert message "Unfortunately, appname has stopped" and then the browser loads.

Why is this occurring and can we remove this error?

Is anything appearing in LogCat? Can you post the error messages?

Is anything appearing in LogCat? Can you post the error messages?

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

Sure! Here it is

+06-12 18:06:13.750: D/AndroidRuntime(1980): Shutting down VM
+06-12 18:06:13.750: W/dalvikvm(1980): threadid=1: thread exiting with uncaught exception (group=0xa4d7fb20)
+06-12 18:06:13.750: E/AndroidRuntime(1980): FATAL EXCEPTION: main
+06-12 18:06:13.750: E/AndroidRuntime(1980): Process: com.akshay.project, PID: 1980
+06-12 18:06:13.750: E/AndroidRuntime(1980): java.lang.RuntimeException: Unable to resume activity {com.akshay.project/com.akshay.project.MainActivity}: android.util.SuperNotCalledException: Activity {com.akshay.project/com.akshay.project.MainActivity} did not call through to super.onResume()
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.os.Handler.dispatchMessage(Handler.java:102)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.os.Looper.loop(Looper.java:136)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.app.ActivityThread.main(ActivityThread.java:5017)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at java.lang.reflect.Method.invokeNative(Native Method)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at java.lang.reflect.Method.invoke(Method.java:515)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at dalvik.system.NativeStart.main(Native Method)
+06-12 18:06:13.750: E/AndroidRuntime(1980): Caused by: android.util.SuperNotCalledException: Activity {com.akshay.project/com.akshay.project.MainActivity} did not call through to super.onResume()
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.app.Activity.performResume(Activity.java:5312)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
+06-12 18:06:13.750: E/AndroidRuntime(1980):    ... 12 more
+06-12 18:06:17.994: I/Process(1980): Sending signal. PID: 1980 SIG: 9
Ben Jakuben
Ben Jakuben
Treehouse Teacher

What does your code look like now? Did you update onResume() and call super.onResume(); like in my earlier reply?

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

Well, now it's working. Thanks a lot Ben and Steve. BTW, you have put an extra "}" at the end.

Also I'd like to how can we add a loader or some different page such that, the MainActivity doesn't gets displayed for a second when the app starts.

It appears partly for a second and then the browser starts.

Best, Akshay

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Cool! That's a good question for another thread, but you could put a fullscreen image in there, kind of like how we set the background for the Crystal Ball app. Just add an ImageView and make it cover the whole screen.

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

I also thought that but then it won't be a good option if we consider UX. The image might be displayed for a second or maybe two and the user might get confused. Instead having a loading bar along with the image would be a good option I believe.

Also in the current app, there is one issue.

When the user clicks on the "go back arrow" button, then at that point. Two instances are running: The browser and the app itself(though the user will be returned to the main menu).

Therefore, what changes should we make in terms of completely exiting the original app and letting the browser run when the user clicks on the go-back button.

Akshay Shivpuri
Akshay Shivpuri
2,917 Points

Well I've uploadedthe screenshot here. Part 1: http://goo.gl/txUAmd Part 2(starts under the arrow): http://goo.gl/HXoQwe

That's over my head. It looks like super onResume() is getting bypassed which is throwing an exception. I'm not sure, though. Sorry!