Reload the same activity when press Home - android

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();
}

Related

how to set switch button to manual mode in android

My application contains two activities :
First activity contains :
1.different types of modes
2.intensity
3.CCT
Inside of the if condition not going control.if am selecting the seekbar the it should return true.if it is true means should move to next activity.
can any one help me
mColorTemp = (SeekBar) findViewById(R.id.intensity1);
mScheduler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean swichAction=false;
if(mColorTemp.isSelected()==true){
swichAction=true;
Intent intent = new Intent(mContext, SchedulerActivity.class);
intent.putExtra("swichAction",swichAction);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
});
Along with this i have button named as Scheduler.
Now am not selecting anyone from first activity and press on the scheduler then it will move to the second activity.In the second activity should show labeled switch on to auto.
If am selecting anyone from first activity then should turn to Manual
Second Activity contains :
Labeled switch in that text contains Auto/Manual
Can any one please help me how to do it.
You can't use mColorTemp.isSelected() to do that. Instead, you has to plug a listener on value changed. If value is changed by user, manual mode can be activated.
mColorTemp.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mManualActivated = true;
}
}
Then, you can use Extras to pass variables from an Activity to another.
How to "put"
Intent intent = new Intent(mContext, SchedulerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
intent.putExtra("extra_mode", mManualActivated);
startActivity(intent);
Then, to retrieve the value, add this in the SchedulerActivity.onCreate() :
Boolean manualActivated = false;
Bundle extras = getIntent().getExtras();
if(extras != null) {
manualActivated = extras.getBoolean("extra_mode");
}

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
}

Stop media player MP3 when back button pushed

My code works fine except when you press the back button.
The MP3 file called "testing" stops playing when I push the stop button and the audio check screen goes back to my menu screen. Perfect... all works great until I test the code on my phone. When I exit using the the phone's back button on my Android phone (not the stop button on my app) the media player keeps playing. I tried all the code and viewed every pertinent question. I implemented code using onStop onPause finish keyevent.KEYCODE_BACK and so forth, nothing works. I'm stumped.
Here is my code. How can I stop this MP3 by pressing the Android back button relative to this code?
public class Audio_Check extends Activity{
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
public class Audio_Check extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_check);
final MediaPlayer buttonSound = MediaPlayer.create(Audio_Check.this, R.raw.testing);
Button stbutton = (Button) findViewById(R.id.startbutton);
stbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
}
});
Button spbutton = (Button) findViewById(R.id.stopbutton);
spbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.stop();
buttonSound.release();
Intent openE1 = new Intent("com.myapp.mysite.MENU");
startActivity(openE1);
}
});
}
}
Put this method,
#Override
protected void onDestroy() {
super.onDestroy();
buttonSound.stop();
buttonSound.reset();
buttonSound.release();
buttonSound = null;
}
Hope it will work.
I implemented code using onStop onPause finish keyevent.KEYCODE_BACK
and so forth, nothing works
There's no need to implement this in onPause or onStop, just override the back button and stop your media player and finish your activity.
#Override
public void onBackPressed(){
if(buttonSound != null && buttonSound.isPlaying())
buttonSound.stop();
finish();
}
If you're using an API lower than 5 :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(buttonSound != null && buttonSound.isPlaying())
buttonSound.stop();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
I couldn't sleep. Kept coding and researching all night until I got it to work.
IT WORKED! But I had to figure out the next step based on verbose error messages while running. I had to take out the "final and MediaPlayer" or I would get a forced shutdown from Eclipse when I ran it through the simulator. No errors showed up while compiling the code.
MediaPlayer buttonSound ; // declared above first #Override
// code after that
// changed code from this
final MediaPlayer buttonSound = MediaPlayer.create(Audio_Check.this, R.raw.testing);
// to this
buttonSound = MediaPlayer.create(Audio_Check.this, R.raw.testing);
// and it works
Thank you
NOW IT WORKS... Here was the problem
note where the buttonSound.start(); was placed in my original code? It was above the });
When I put it below the }); it worked. Apparently it was protected before but now it works fine.
buttonSound = MediaPlayer.create(Audio_Check.this, R.raw.testing);
Button stbutton = (Button) findViewById(R.id.stbutton);
stbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
buttonSound.start();
// onDestroy goes right here

Set Intent to back key in TabActivity

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);
}

Intercepting keyevents to MediaController?

Basically, I'm trying to recreate the functionality of the MediaController in the native Music app (that I see on 2.2), where the back button immediately backs out of screen, instead of hiding the MediaController. There doesn't seem to be any good way to set a keylistener or override a method to intercept these keyevents though.
Any ideas?
You can try something like
mMediaController = new MediaController(this) {
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.dispatchKeyEvent(event);
}
}
or the dispatchKeyEvent like
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
ContentActivity.this.dispatchKeyEvent(event);
}

Categories

Resources