I am running a service that starts a new activity when specific
applications are launched.
For example, when I launch sms application, my service detects it by
checking a top activity package name and starts a new activity.
But the problem is that after starting a new activity, when I finish
that activity and press BACK button from sms application to go back to
Home screen, it does not finish my sms application.
Even though the screen is at home(launcher), when I check top activity
name, sms app is running as the top activity, which means sms app is
not finished after pressing BACK button.
I use Intent.FLAG_ACTIVITY_NEW_TASK intent flag for starting a new
activity and finish() to finish it. Does anyone have an idea why my
BACK button does not finish sms application in this case?
thanks,
777
From what I've seen, the back button will halt the current activity, whatever it's doing. If you absolutely need to finish it off, take a look at the lifecycle of an activity, and perhaps put some code into the onPause() and onStop() functions.
OK if it doesnt workout.. try overriding the OnBackPressed method
and put finish() in that.. hope this helps
If you want to finish the activity when user navigate to back through device So you can use this code it is more helpful.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
I did something like that in my activity:
#Override
public void onBackPressed(){
AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
alert.setTitle("Wylogowanie i wylaczenie.");
alert
.setMessage("Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//DO SOMETHING YOU NEED eg. destroy/close DB, stop services, and move the task to back
db.getWritableDatabase(); //getDB
db.destroyDB(context); //Destroy it
db.close(); //and close
stopService(new Intent(getBaseContext(), SVCKeepLogged.class)); //stop some service
moveTaskToBack(true); //hide activity (this do not kill it - you can use finish() here)
//finish();
}
})
.setNegativeButton("NOOO!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
//Keep app alive.
dialog.cancel();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
try this
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
// TODO
}
#Override
public boolean onOptionItemSelected(MenuItem item){
if(item.getItemId() == android.R.id.home){
finish();
}
}
Related
Hello I am making boxing countdown timer and I have the ring that plays at the end and start of each round. I would like to make "end" button but when I am using Intent like bellow I go to previously activity but mp3 files still working in background. How to end all tasks and go to previously activity without any mp3 and tasks in background?
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialog=new AlertDialog.Builder(Main3Activity.this);
dialog.setMessage("Czy napewno chcesz wyjść z trenignu?");
dialog.setPositiveButton("Tak", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(Main3Activity.this, Main5Activity.class);
startActivity(intent);
}
});
dialog.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialogg=dialog.create();
dialogg.show();
}
});`
removeEventListener()
use HTML DOM removeEventListener() Method
btn1.removeEventListener("click", function(){});
see: https://www.w3schools.com/JSREF/met_element_removeeventlistener.asp
Your question looks incomplete or incorrect to me.
If you want to go to previous activity from the activity stack, then you should not use intent to start the previous activity, rather finish the current Activity.
If you want to stop playing the bell ring once the user is not interacting with the Main3Activity or when Main3Activity is not visible to the user. Then best practice is to write a code in the onStop() method of the Main3activity to check for the object which is responsible for playing the sound and safely call stop or destroy the object.
simply finish the activity which can destroy the instance of activity and everything that is running on that particular activity. simply write this line after startActivity
finish();
public boolean onKeyDown(int keyCode, KeyEvent event){
if (isSub2&&keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(ctxx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
isReturning = true;
return false;
}
else {
return super.onKeyDown(keyCode, event);
}
}
}
There are two Activities Main--Sub2.
When you push a button in Main you can go to Sub2.
This code is in Sub2. I want to use back button on the bottom to make the MainActivity put on the top of stack not killing Sub2.
When I run it on the phone it works all right at first,
but after few more times of going back in Sub2 and going to Sub2 again
the back button stops working.
I don't know what is making the back button freeze.. any ideas?
ps) i've tried using handlers inside the method and overriding onBackButtonPressed() instead of using onKeyDown..
but no difference at all..
Not sure why your button freezes. It would be helpful to see what you are doing in the main activity. Here's an example that works for me and does not freeze:
You can put this in the main activity:
#Override
protected void onStart() {
super.onStart();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SubActivity.class);
//****** Uncomment the following line if you want to re-use the subactivity instead of launching a new one
//intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
});
}
And this in the sub-activity:
#Override
public void onBackPressed() {
//super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
There are the 3 basic keys in an Android device.
1. Home button (middle button)
2. Menu button (left most button)
3. Back Button (right most button)
I'm trying to create a code the use will press the Back button and a AlertDialog will appear before return to the previous activity.
How can I handle the back button and do I have to insert it on the onCreate on the Java File of an Activity?
Thank You!
All (4) buttons are handled differently.
The home button and running apps buttons are completely outside your control.
To get the BackButton press, override this function.
#Override
public void onBackPressed(){
//Do Stuff
}
To get the Menu button key press, use the following
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
After you show the user your alert dialog, you can either send a real BackPress to the system using super.onBackPressed(), or you can manually finish() your activity.
#Override
public void onBackPressed(){
AlertDialog.Builder build=new AlertDialog.Builder(currentactivity.this);
build.setMessage("Are you sure you want to return?");
build.setCancelable(false);
build.setTitle("Note");
build.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
Intent i =new Intent(currentactivity.this,anotheractivity.class);
startActivity(i);
//or just type finish();
}
});
build.setNegativeButton("no", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
});
AlertDialog alert=build.create();
alert.show();
}
I have looked at quite a few posts on here and haven't been able to get anything to work. I am trying to have either an AlertDialog or an Activity class (set to a Theme.Dialog style) prompt users to see if they want to exit a side Activity and go back to the Home activity. Everything I have tried just doesn't seem to work.
[NOTE: All of the following examples were tried as the first lines in...]
#Override public void onBackPressed(){}
I have tried -
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
which closes both the current Activity and the Home menu Activity (the next Activity in the stack), while -
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Session.closing = true;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Session.closing = false;
}
});
AlertDialog alert = builder.create();
alert.show();
closes the current Activity and creates a pop-up over the Home activity. This is the outcome of most of the other things I have tried, like...
super.onBackPressed();
startActivity(new Intent(this, CloseActivityView.class));
Are there any tricks to getting onBackPressed from dumping your current child Activity?
First of all, don't call super.onBackPressed(); - this will call finish() and your current activity will be removed.
Secondly, this:
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
creates an intent that launches the Home screen. (See the Intent docs)
What you could do is put something like this in your onBackPressed override:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(this, HomeActivity.class));
finish();
}
})
.setNegativeButton("No", null); // I think passing null here is OK.
AlertDialog alert = builder.create();
alert.show();
Then specify your HomeActivity as launchMode="singleTask" in your manifest, as detailed in the Tasks and Back Stack docs. You could do the same thing by specifying the FLAG_ACTIVITY_NEW_TASK flag on the Intent for navigating to your HomeActivity.
If you want to show a confirmation dialog on back button press then override the onBackPressed and show the AlertDialog. If user confirms then call dialog.dismiss() to dismiss the dialog and then if you want to exit the app and go to the home screen then finish this activity and start the homescreen intent and the code you have tried for this is right. or if you want to go back to an activity within your app then you can start that activity with FLAG_ACTIVITY_CLEAR_TOP. Don't call super.onBackPressed() in your overriden version unless you want to finish the current Activity.
Okay. Three things to say:
if your home activity contents are not changed after navigating away from it, and doing some operations, then, i recommend that when you are navigating awway from homw screen, don't call finish(). Let the home screen be in the activity stack. So when the child activity has to navigate back to home activity, it just needs to finish its own activity, and Home screen will appear after that.
Your code is somewhat perfect. All you have to do is finish the activity when yes button is pressed on AlertDialog
And on Homescreen, inside onBackPressed() or whatever you prefer, just show the AlertDialog (that you have shown above), and you can code for Yes and No buttons
In a given activity, an AlertDialog takes the user into WiFI settings. Then, the user presses the back button to return to said activity.
However, as soon as the back button has been pressed I need to make a method call. Please note that I cannot simply add the method after the following code in the activity, as this will impact the time the user has to interact with the AlertDialog instance.
The method call needs to happen as soon as the back button has been pressed form the WIFI settings menu. Please inform me of how I can implement this.
Here is the code:
alertDialog.setPositiveButton("Settings", new dialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);
}
});
You can Override the onResume() method of the calling Activity. As soon soon as the user presses the "back" button the onResume() method is sure to get called so you should be able to put your method call here
class member
private static final int WIFI_REQUEST = 1234;
Use startActivityForResult
alertDialog.setPositiveButton("Settings", new dialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivityForResult(intent, WIFI_REQUEST);
}
});
In the activity class
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode)
{
case WIFI_REQUEST:
// Call your method here
break;
}
}
private boolean inwifisettings;
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
inwifisettings = true;
startActivity(intent);
}
#Override public void onWindowFocusChanged(boolean hasFocus)
{
if(inwifisettings & hasFocus)
{
doSomething();
inwifisettings = false;
}
}
You should not use onResume() or startActivityForResult()/onActivityResult() for this purpose. Quoting the Android documentation: http://developer.android.com/reference/android/app/Activity.html
public void startActivityForResult (Intent intent, int requestCode, Bundle options)
Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.
public void onWindowFocusChanged (boolean hasFocus)
This is the best indicator of whether this activity is visible to the user.
the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity.