Set Intent to back key in TabActivity - android

I have three Activities - A B and C, of which B is a Tab Activity.
Activity A is launched first, and B is launched from A. I finish Activity A when B is launched using this code
public void onStop() {
super.onStop();
this.finish();
}
Now I want to launch Activity C when back key is pressed in B.
I tried overriding back key using this code
#Override
public void onBackPressed() { this.getParent().onBackPressed();
}
This doesn't Help as the Parent Activity is finished while launching the Child Activity. What actually happens when I press back key is that the Activity exits to the Home screen.
I tried overriding the back key and setting an Intent to it
#Override
public void onBackPressed() {
Intent backIntent = new Intent();
backIntent.setClass(this, main.class);
startActivity(backIntent);
}
This also doesn't help me.
What could be a possible solution to this problem, How can I launch Activity C when back key is pressed ?

First you should not finish the activity A when Activity A stops this is completely wrong approach instead of it you have to finish activity when you start activity B.
For example
Intent i = new Intent(this, B.class);
startActivity(i);
finish();
Now you want to start the activity C when user press the back button so use the below code.
#Override
public void onBackPressed() {
Intent backIntent = new Intent(this, C.class);
startActivity(backIntent);
super.onBackPressed();
}

You have to override onKeyDown
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_BACK)
{
//Do your code here
}
return super.onKeyDown(keyCode, event);
}
}
This will be called when user pressed Device Hard Back Button.
To navigate to next activity:
StartActivity(new Intent(getApplicationContext(),main.class));

Override the below method and import event.....
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_BACK)
{
//Write your intent or other code here
}
return super.onKeyDown(keyCode, event);
}

Related

Back press event of device to dismiss a window

I have created a window and i am showing it on screen through Broadcast receiver.But the problem is that it appears on the screen and i want to dismiss it once the back button is pressed.I am unable to get the event of button press on this view.My code for back press looks as follows-
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KEYCODE_BACK) {
Log.d("LOG", "back button is pressed");
}
return true;
}
});
But Nothing is happening.I tried to do the same through DISPATCHKEY but it was also of no use.Please help me what i am not figuring out.Wouldn't this work on the view.?
Maintain global reference for Window and override onBackPressed()
Try this:
#Override
public void onBackPressed() {
if (view != null && view.isShowing()) {
view.dismiss();
} else {
super.onBackPressed();
}
}

Avoid Alert Dialog getting closed when back button pressed?

First thing I want to say, this was done by seeing a tutorial. Here is the Custom Alert Dialog activity part I am calling from a broadcast receiver. The only problem is the back button click. Once the Alert dialog activity got started, when I press the back button it is getting closed.
public class AlertDialogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setFinishOnTouchOutside(false);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
setContentView(R.layout.activity_inmsgdialog);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_SHORT).show();
}
}
I have tried onBackPressed and I'm able to see the toast message but the activity is getting closed.
See here:
#Override
public void onBackPressed()
{
super.onBackPressed(); //Remove this line
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_SHORT).show();
}
Do not call super.onBackPressed(); code if you want to disable back button for activity. So remove this line. Hope it helps.
You can use the below option to handle back button press
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//your code
return true;
} else {
}
}
Don't propagate the event and you should be good.
#Override
public void onBackPressed()
{
//don't call super
}

Reload the same activity when press Home

I have an application. I tried to disable home button. Many people said that it doesn't possible to do in android 4.0 and above. So i decided to reload the same activity when press home button. I followed the below code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_HOME) {
System.out.println("==============================");
Intent i = new Intent(getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true;
}
return false;
}
I can't get any response when press home button. Can you tell me what's my wrong?
This key cannot be intercepted, so KEYCODE_HOME won't be send to you.
Its impossible to override the home button.
public static final int KEYCODE_HOME
Added in API level 1
Key code constant: Home key. This key is handled by the framework and is never delivered to applications.
Source:
http://developer.android.com/reference/android/view/KeyEvent.html
This is a refresh button method, but it works well in my application. in finish() you kill the instances
refresh = (Button)findViewById(R.id.refresh);
refresh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onRestart();
}
});
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Intent i = new Intent(lala.this, lala.class); //your class
startActivity(i);
finish();
}

My preferences does not show up after pressing settings button

So, I am currently trying to edit the Snake game that android's api provides. I am trying to make it so that after pressing menu->settings->resume, it would resume the game. However, instead of going all the way to resume, after I press settings, it quits and resumes from there. I am using Intents to resume the program.
public boolean onOptionsSelected(MenuItem menu){
switch(menu.getItemId()){
case R.id.settings:
Intent prefActivity = new Intent(this,MyPreferences.class);
startActivityForResult(prefActivity, KEY_RESUME_RESULT);
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
switch(requestCode){
case KEY_RESUME_RESULT:
if(resultCode==RESULT_OK){
if(mSnakeView.getMode() == SnakeView.PAUSE)
this.mSnakeView.setMode(SnakeView.RUNNING);
}
}
}
This is in MyPreferences.class
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.snake_preferences);
resume = (Preference)findPreference(getString(R.string.key_resume));
onPreferenceTreeClick(this.getPreferenceScreen(),resume);
}
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferencescreen,Preference preference){
super.onPreferenceTreeClick(preferencescreen,preference);
Intent intent = new Intent();
if(preference == resume){
setResult(Activity.RESULT_OK,intent);
finish();
}
return true;
}
You're explicity calling the method that resumes the game inside onCreate(), which is probably why the game is resuming as soon as you launch settings. I'd suggest not using the method and instead setting up a click listener for your preference:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.snake_preferences);
resume = (Preference)findPreference(getString(R.string.key_resume));
resume.setOnPreferenceClickListener (new Preference.OnPreferenceClickListener()
{
#Override
public void onPreferenceClick(Preference preference)
{
setResult(Activity.RESULT_OK,intent);
finish();
}
});
}
Also, based on your code onOptionsSelected() doesn't explicitly pause the game (however the UI should be paused considering that onPause() will be called), you might want to look into that.

back button error, action down

I have a problem with my code about back button. I've tried a lot of answers here in this site.
I have the mainActivity that calls a second activity with startActivityforResult. This second activity starts bluetooth and show a list of the bonded devices, but if I press the backbutton it stops the app with an error.
public class Main extends Activity implements OnSeekBarChangeListener{
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String address = data.getExtras().getString(BondedDevices.DEVICE_ADDRESS);
if (resultCode==Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Finalizando dispositivos pareados", Toast.LENGTH_SHORT).show();
return;
}
switch (requestCode) {
case DISPOSITIVOS_PAREADOS:
if(resultCode==Activity.RESULT_OK){
mConnectThread = new ConnectThread(address);
mConnectThread.start();
estado = EST_CONECTADO; //informa que esta conectado
atualizaEstado();
break;
}
return;
}
}
But when I am in the second activity, and try to back to the mainactivity just pressing the back button, I get an error on main activity and my app return an error:
public class BondedDevices extends ListActivity {
....
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(D) Log.e(TAG, "+++ ON BACK PRESSED +++");
setResult(Activity.RESULT_CANCELED);
this.finish();
}
or like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
return true;
}
}
return false;
}
I have tried a lot of different code, but it still doesn't work. Please someone help me. Thank you.
you should call super.onBackPressed();
like this
#Override
public void onBackPressed() {
super.onBackPressed();
if(D) Log.e(TAG, "+++ ON BACK PRESSED +++");
setResult(Activity.RESULT_CANCELED);
this.finish();
}
When you return from the second Activity, you have not set an Intent as the data for the result in the back button case, yet the first thing you do before checking the value of resultCode for RESULT_CANCELED is try to obtain the address from the Intent, which is null so this will cause a NullPointerException.
You need to reorder the lines in your onActivityResult() to look more like this:
if (resultCode==Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Finalizando dispositivos pareados", Toast.LENGTH_SHORT).show();
return;
}
//Do this after checking for cancel
String address = data.getExtras().getString(BondedDevices.DEVICE_ADDRESS);
/* The rest of the existing code */
Also, you should NOT override BOTH the onBackPressed() and onKeyDown(), stick with one of them. You're only causing confusion about which code path gets called first. Even in the case where the result is set with a blank Intent, you will still get a NullPointerException in your existing code because the extras bundle of that Intent will still be null.

Categories

Resources