0

i need help in my app which shows a toast when home button is pressed, back button is pressed and it check if the phone have got the nav bar

MainActivity.java

package com.example.myapp;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        Toast.makeText(getApplicationContext(), "goodbye! (home button pressed)", Toast.LENGTH_LONG).show(); //doesn't work here
        finish();
        return true;
    }
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Toast.makeText(getApplicationContext(), "goodbye! (back button pressed)", Toast.LENGTH_LONG).show();
        finish();
        return true;
    }
        return false;
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);      

    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);

    if (hasBackKey && hasHomeKey) {
        // no navigation bar
        Toast.makeText(getApplicationContext(), "no nav bar", Toast.LENGTH_LONG).show();
    } else {
        // navigation bar
        Toast.makeText(getApplicationContext(), "nav bar", Toast.LENGTH_LONG).show();
    }
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
        }
    }

}

I have got a nexus 5 so it has the nav bar, and I think this is the problem

how can I fix the prolem?

nexus 5, android 4.4.3

thanks

user3744384_
  • 337
  • 3
  • 15

1 Answers1

3

The framework never sends KEYCODE_HOME to apps. Right now your best indicator of when the user leaves your app (as opposed to a single activity), is listening for TRIM_MEMORY_UI_HIDDEN in onTrimMemory(int).

public class MainActivity extends Activity {
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_UI_HIDDEN) {
            Context ctx = getApplicationContext();
            Toast.makeText(ctx, "gone", Toast.LENGTH_SHORT).show();
        }
    }
}

Also you should not finish() just because the user leaves, then when the user gets back the activity will always have to be recreated from scratch, without state.

sergio91pt
  • 1,459
  • 18
  • 21
  • @user3744384 ... Read, click the link, test the code, and... if it doesn't help explain what are you trying to solve. You're never going to get a KEYCODE_HOME callback. – sergio91pt Jun 16 '14 at 10:43