Android back button Not working - android

public class Myactivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
}
#Override
public void onDestroy() {
this.finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.myactivity, menu);
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
super.loadUrl("file:///asset/www/index.html");
}
return super.onKeyDown(keyCode, event);
}
In Above I need to go to the index.html file when the back button is clicked.But whwn i try it i get folowning error.
11-02 12:25:00.677: E/WindowManager(24344): Activity org.apache.cordova.example.Myactivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#421fad78 that was originally added here
11-02 12:25:00.677: E/WindowManager(24344): android.view.WindowLeaked: Activity org.apache.cordova.example.Myactivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#421fad78 that was originally added here

the back button is supposed to finish the activity. Calling super.onKeyDown(keyCode, event) you are asking android to call, currentActivity.finish();.

override the onBackPressed() method. put your code in it.,it will work

Related

Menu key is triggered twice android

If I the press menu key, it is triggered twice in all of my app's Activities
I tried to override onKeyUp and onKeyDown but no use any suggestions as to why is this happening?? Thanks in advance code goes on like this..
public class MainActivity extends ActionBarActivity {
private MainActivityDrawer mainActivityDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mainActivityDrawer = (MainActivityDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_main_activity_drawer_in);
mainActivityDrawer.setUp(R.id.fragment_main_activity_drawer_in, (DrawerLayout) findViewById(R.id.mainDrawer1), toolbar);
new clicklisteners().execute();
DrawerLayout drawerLayout = ((DrawerLayout) (findViewById(R.id.mainDrawer1)));
drawerLayout.setStatusBarBackground(R.color.PrimaryColorDark);
registerReceiver(err, new IntentFilter("ERROR_LOG_BUTTON_CLICKED"));
//toolbar.setOnKeyListener(this);
}
#Override
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
return false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP)
{
if(event.getKeyCode()==KeyEvent.KEYCODE_MENU)
{
ShowToast("Hello");
return true;
}
}
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK)
{
if(mainActivityDrawer.isOpened())
{
mainActivityDrawer.close();
}
else
{
finish();
}
}
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
......
}
It seems the library 'com.android.support:appcompat-v7:22.0.0' has a problem with the menu button ,When i made that v7:22.0.0 to v7:21.0.0 the menu key works properly

How to disable the home button in android 4.0+ versions

I tried following code but it is not getting to disable the home button above 4.0 versions in android
public class DHActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dh);
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dh, menu);
return true;
}
#Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.i("TESTE", "BOTAO HOME");
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
}
}
In the lower versions of android you can catch the home button , in later versions for security reason Google not allowing to override home button press.

android openPanel menu is empty

I have a tabActivity with multiple activities in one tab.
The following code work on android 2.3 but it's not working on android 4.2
ActivityStack.java
public class ActivityStack extends ActivityGroup {
..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// // what is the current activity?
menu.add(0, 0, 0, "holder");
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// start a new
menu.clear();
// add some menu options
getLocalActivityManager().getCurrentActivity().onPrepareOptionsMenu(menu);
return super.onPrepareOptionsMenu(menu);
}
..
Activity1Tab1.java
here I have a button from where I am calling Activity2Tab1.java onClickListener
Intent acIntent = new Intent();
acIntent.setClass(getParent(),
Activity2Tab1.class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("SecondActivity", acIntent);
Activity2Tab1.java
..
here I have multiple layouts...defined
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{ //add menus or inflate here
Log.d(TAG, "onPrepareOptionMenu");
if (!isMainMenuVisible) {
pushMainMenuUp();
} else {
pushMainMenuDown();
}
return true;
}
Need some help!!!
Neither in Activity1Tab1 or in Activity2Tab1 the override method onKeyUp() IS NEVER CALLED. The only called methods are from StackActivity. WHY?
After some research done and some thinking I manage to make this code work.
Instead of using onPrepareOptionMenu(menu) and onCreateOptionMenu(menu) I've override the following method in StackActivity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, keyCode+"");
getLocalActivityManager().getCurrentActivity().onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
}
and in Activity1Tab1 and Activity2Tab1 I had the method:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, ""+event.getAction());
if (keyCode == KeyEvent.KEYCODE_MENU) {
Log.d(TAG, "MENU_BUTTON_PRESSED");
if (!isMainMenuVisible) {
pushMainMenuUp();
} else {
pushMainMenuDown();
}
}
return super.onKeyUp(keyCode, event);
}

Prevent to cancel Action Mode by press back button

Action mode started by calling getActivity().startActionMode(calback); is automatically canceled after back button pressed. Is possible avoid this behavior? I need to do another operation after back button was pressed in some situation during action mode.
This is an interesting problem. When the ActionMode is active the back key event is consumed internally. The event is not propagated to either onBackPressed() or onKeyUp(int keyCode, KeyEvent event) callbacks.
Fortunately, you can use dispatchKeyEvent(KeyEvent event) which is still called.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(mActionModeIsActive) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// handle your back button code here
return true; // consumes the back key event - ActionMode is not finished
}
}
return super.dispatchKeyEvent(event);
}
You might wonder what will be the behavior in case you have a submenu in the ActionMode and you close it with the back key. In this case dispatchKeyEvent() is not called so you can safely use the code.
The above code works also with ActionBarSherlock. The only problem I found is on Android 3.1 device when the native ActionMode is used, in this case the dispatchKeyEvent() is not called. Use ActionBarSherlock's ActionMode to solve it.
Suggested solutions did not work for me. So I decide to create back event manually. I needed this event in my fragment so I created BaseFragment that all my fragments will extend.
public abstract class BaseFragment extends Fragment {
private ActionModeState actionModeState = ActionModeState.ITEM_NOT_CLICKED;
protected enum ActionModeState {
ITEM_NOT_CLICKED, ITEM_CLICKED
}
protected void onActionItemClicked() {
actionModeState = ActionModeState.ITEM_CLICKED;
}
protected void onDestroyActionMode() {
if (actionModeState == ActionModeState.ITEM_NOT_CLICKED) {
onActionModeBackPressed();
} else {
// reset state
actionModeState = ActionModeState.ITEM_NOT_CLICKED;
}
}
protected void onActionModeBackPressed() { }
}
Main fragment
public class YourMainFragment extends BaseMapFragment {
#Override
public void onActionModeBackPressed() {
// you code for action mode back button
}
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
YourMainFragment.this.onActionItemClicked();
....
}
#Override
public void onDestroyActionMode(ActionMode mode) {
YourMainFragment.this.onDestroyActionMode();
...
}
};
Create your own Window.Callback and intercept event before it is passed to AppCompatDelegateImplBase.
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//default delegate
final Window window = getActivity().getWindow();
mWindowCallbackDelegate = new WindowCallbackDelegate(window.getCallback(), this);
window.setCallback(mWindowCallbackDelegate);
return true;
}
In your own delegate :
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
return myWindowDelegate.dispatchKeyEvent(event) || mOriginalWindowCallback.dispatchKeyEvent(event);
}
When you destroy action mode, restore reference to previous delegate
#Override
public void onDestroyActionMode(ActionMode mode) {
Window.Callback originalWindowCallback = mWindowCallbackDelegate.getOriginalWindowCallback();
if (originalWindowCallback != null) {
getActivity().getWindow().setCallback(originalWindowCallback);
}}
You own delegate signature:
public class WindowCallbackDelegate implements Window.Callback {...}

Calling Custom Options menu when webview is running

I want to call my function onCreateOptionsMenu when the menu button is clicked when my webview is running. Right now when you click the Memnu button when the Webview is runing it displays some default menu not my custom menu.
How would I modify my code below to disable the default menu and display my custom menu? When the web view is not running it shows my custom menu when you click the menu button.
public class WebViewActivity extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
final Activity mActivity = this;
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
// SETS DEFAULT MODE TO LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://Google.com");
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
mActivity.setTitle("Google.com");
mActivity.setProgress(progress * 100);
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// do your menu stuff here
return true;
}
else
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
}
You have to override the onKeyDown function, to intercept the Menu key. Otherwise the webview will consume it. Something like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// do your menu stuff here
return true;
}
else
return super.onKeyDown(keyCode, event);
}

Categories

Resources