Frustrated Developer
Frustrated Developer

Reputation: 131

BackKey button function in Android

this is my problem. Everytime i hit the backbutton on any android device, the application automatically closes. Is there any way i could make the back button of the device load the previous activity?

Here are the codes

package com.phonegap.mobilemone;
import com.phonegap.DroidGap;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;

public class KeyBoard {

        private WebView mAppView;
        private DroidGap mGap;

        public KeyBoard(DroidGap gap, WebView view)
        {
            mAppView = view;
            mGap = gap;
        }

        public void showKeyBoard() {
            InputMethodManager mgr = (InputMethodManager)     mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
            // only will trigger it if no physical keyboard is open
            mgr.showSoftInput(mAppView, InputMethodManager.SHOW_IMPLICIT);
            
            ((InputMethodManager)     mGap.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(mAppView, 0); 

        }
        
        public void hideKeyBoard() {
            InputMethodManager mgr = (InputMethodManager)     mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromWindow(mAppView.getWindowToken(), 0);
        }
}

Upvotes: 1

Views: 911

Answers (2)

Pankaj Kumar
Pankaj Kumar

Reputation: 82958

In android, when you press back button you goes to previous Activity, But when developing PhoneGap app, you are working on single Activity (this is the reason when you press back button, application gets close). You need to implement your own listener which will listen back key press.

How to create Android Back Button handler:

phonegap 0.9.5 and later:

// This is your app's init method. Here's an example of how to use it

function init() {
    document.addEventListener("deviceready", onDR, false);
} 
function onDR(){
    document.addEventListener("backbutton", backKeyDown, true);
    //boot your app...
}
function backKeyDown() { 
    // do something here if you wish
    // alert('go back!');
}

phonegap 0.9.4 and earlier:

// This is your app's init method. Here's an example of how to use it

function init() {
    document.addEventListener("deviceready", onDR, false);
}
function onDR(){
    BackButton.override(); 
    document.addEventListener("backKeyDown", backKeyDown, true);
    //boot your app...
}
function backKeyDown() { 
    // do something here if you wish
    // alert('go back!');
}

Here are References :

Back button handler

CATCHING ANDROID’S BACK BUTTON IN PHONEGAP

Upvotes: 1

user1213202
user1213202

Reputation: 1305

Try with this...Specify ur Target Activity class name using intent.

    @Override
public void onBackPressed() {
    Intent BackpressedIntent = new Intent();
    BackpressedIntent .setClass(getApplicationContext(),TargetActivity.class);
    startActivity(BackpressedIntent );
    finish();
}

Upvotes: 1

Related Questions