Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android finish current activity causes app close

I am executing maswebview class and I would like to finish only this activity. I tried maswebview.this.finish() but when executed, app is been closed. Then if I set a new view for the tab content, it is loaded properly and webviewmas dissapears but just for a while, then appears again fitting fullscreen. How to finish maswebview completely? ThanK you

public void onClick(View arg0) 
            {
                /*
                Intent intent = getIntent();
                intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
                startActivityForResult(intent, 1);
                Intent intentmas = new Intent (maswebview.this, mas.class);
                intentmas.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
                intentmas.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                intentmas.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                intentmas.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                View vista = getLocalActivityManager().startActivity("maswb", intentmas).getDecorView();
                setContentView(vista);  */

                maswebview.this.finish();
like image 894
Jaume Avatar asked Jun 18 '12 17:06

Jaume


People also ask

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

What happens when you call finish () inside onCreate ()?

You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

How do you end an activity using context?

you can call finishActivity(1) to finish any activities started with that request code, like this: ((Activity)getContext()). finishActivity(1);

Does finish call onPause?

Android will generally call onPause() if you call finish() at some point during your Activity's lifecycle unless you call finish() in your onCreate() . Run, and observe that your log will only contain "onDestroy". Call finish() almost anywhere else and you'll see onPause() called.


1 Answers

Do you have any other activities of your app in the stack by the time you call finish()? If you don't, you'll want to start the desired activity instead of finishing the current one.

But actually it seems to me that you're trying to accomplish something that can be done simpler. Can you provide more info on the task at hand and your app structure you're trying to go about it with?

From what you said, it seems like you have tabbed UI and you're trying to show a webview in one of the tabs, then hide it.

First, I don't see why you want the webview in a separate activity. Seems to me you could just have it in layout of one of the tabs and just call setVisibility(GONE) to hide it.

Second - and this is important - looks like you're trying to implement tabs the old way - TabWidget, TabHost, etc. Since Honeycomb has been released, there's much more convenient way to implement tabbed UI - Action Bar Tabs and Fragments - that approach is much more convenient and will render your webview problems obsolete: there's a thing called WebViewFragment which is basically a WebView but smarter - it will handle its own lifecycle with minimum effort required from you (i.e. pause when removed, resume when added). It will take some effort to study up on Fragments, but it's well worth it. You can get Fragments API for pre-Honeycomb android sdks as a static library - it's called android-support-v4 and you can download it in Android SDK Manager.

like image 160
Ivan Bartsov Avatar answered Nov 15 '22 12:11

Ivan Bartsov