0

Below is the pictorial of the problem with my app.

THE APP ON STARTUP:

Snapshot of app while starting

ON 100% PROGRESS:

LOADED PAGE

NOW, IF I HIT BACK KEY AND AGAIN RETURN TO APP:

LOADS AGAIN

WHAT I ACTUALLY WANT: app should resume and not reload when the user returns after pressing back key.

I am sharing the code below. Kindly, suggest a solution.

package com.url.app;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.provider.Settings;



public class MainActivity extends Activity {

 private WebView wv;  

//make HTML upload button work in Webview   
 private ValueCallback<Uri> mUploadMessage;  
 private final static int FILECHOOSER_RESULTCODE=1;

 @Override  
 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {  
  if(requestCode==FILECHOOSER_RESULTCODE)  
  {  
   if (null == mUploadMessage) return;  
            Uri result = intent == null || resultCode != RESULT_OK ? null  
                    : intent.getData();  
            mUploadMessage.onReceiveValue(result);  
            mUploadMessage = null;        
  }  
 }  

 //Check if user is online
 public boolean isOnline() {
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo netInfo = cm.getActiveNetworkInfo();
     if (netInfo != null && netInfo.isConnected()) {
         return true;
     }
     return false;
 }
 //if not online show alertdialog with settings button
 public void showNoConnectionDialog(Context ctx1) {
     final Context ctx = ctx1;
     AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
     builder.setCancelable(true);
     builder.setMessage(R.string.no_connection);
     builder.setTitle(R.string.no_connection_title);
     builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
             ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
         }
     });
     builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
             return;
         }
     });
     builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
         public void onCancel(DialogInterface dialog) {
             return;
         }
     });
     builder.setCancelable(false);
     builder.show();
 }


 //supress lint coz it cries over javascript enabled
@SuppressLint("SetJavaScriptEnabled") @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //if not online then show NO CONNECTION Dialog
    if(!isOnline()) {
       showNoConnectionDialog(MainActivity.this);
    }

    //webview
    wv = new WebView(this);

    //loadurl
    wv.loadUrl(getString(R.string.siteurl));


    //Enable JavaScript
    wv.getSettings().setJavaScriptEnabled(true);

    //Zoom
    wv.getSettings().setBuiltInZoomControls(true);
    wv.getSettings().setSupportZoom(true);

    //progressdialog
    final ProgressDialog pd = ProgressDialog.show(this, "", getString(R.string.loading), true);
    pd.setCanceledOnTouchOutside(false);

    wv.setWebViewClient(new WebViewClient(){
        //hide progressdialog when page loads completely
        @Override
        public void onPageFinished(WebView view, String url) {
            if(pd.isShowing() && pd!=null)
            {
            pd.dismiss();
            }
        }
    });


    wv.setWebChromeClient(new WebChromeClient()  {

        // For Android 3.0+
        public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {  
            mUploadMessage = uploadMsg;  
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
            i.addCategory(Intent.CATEGORY_OPENABLE);  
            i.setType("image/*");  
            MainActivity.this.startActivityForResult( Intent.createChooser( i, getString(R.string.fileselect) ), MainActivity.FILECHOOSER_RESULTCODE ); 
            }

        // For Android < 3.0
        public void openFileChooser( ValueCallback<Uri> uploadMsg ) {
            openFileChooser( uploadMsg, "" );
            }

        // For Android > 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){

            openFileChooser( uploadMsg, "" );
            }        
                });
      setContentView(wv);      
  }
}

PS: the upload button is working in my code, the problem is how do I keep WebView active in background, such that if user starts uploading and exits the app, uploading continues.

UPDATE: I think overriding the back button along with a notification may be useful in this case.

Community
  • 1
  • 1
Chirag
  • 1,189
  • 2
  • 21
  • 43

1 Answers1

0

Catching the back button to behave like home key

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {
         moveTaskToBack(true);
         return true;
     }
     return super.onKeyDown(keyCode, event);
 }
Chirag
  • 1,189
  • 2
  • 21
  • 43