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();
Related
I'm trying to animate my imagebuttons by making them wobble when clicked.
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonStart:
buttonStart.startAnimation(wobble);
Intent i;
i = new Intent(this, CityRendActivity.class);
startActivity(i);
break;
}
}
When I click my Start button, it very slowly starts to wobble, but only appears to shake back and forth like 3 times, and a lot slower than I have defined in the xml file. When I remove the lines that contain the intent code, the wobble works perfectly and the button shakes 5 times quickly (though now of course it doesn't load up my other activity).
So why would the code after the wobble animation effect how it's run? Doesn't each line get resolved individually before proceeding? I don't understand how loading up a different activity would effect the animation that I've set up. Any thoughts? Thanks.
Try adding animation listener for your animation and onAnimationEnd() start your Activity Intent.
wobble.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation){}
#Override
public void onAnimationRepeat(Animation animation){}
#Override
public void onAnimationEnd(Animation animation){
startActivity(new Intent(YourCurrentActivity.this, CityRendActivity.class)); // if the code is in Fragment, then replace YourCurrentActivity.this by getActivity()
}
});
Actually In this code. animation and starting a new activity will execute at a same time. that will disturb the animation in between. so delay the starting of activity with your animation time. so that animation will completed and then your Activity will start.hope this work for you.
new Handler().postDelayed(new Runnable(){
#Overide
void run(){
startActivity(new Intent(this, CityRendActivity.class));
}
},time_ofanimation);
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.
I have "MainMenuActivity" in my application, from which I want to log out. After pressing the back button, this activity should start "Logout activity", which does some logout stuff and then finishes the application.
Method called onBackPressed() from MainMenuActivity:
public static void logoutAction(final AbstractActivity activity) {
Builder dialog = new AlertDialog.Builder(activity);
dialog.setPositiveButton(R.string.dialog_btn_yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent();
i.setClass(activity, iess.student.login.LogoutActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
activity.finish();
}
});
dialog.show();
}
Then, "LogoutActivity" executes AsyncTask, which at the end of its work calls finish() on LogoutActivity.
My problem is, that if other activities were launched before MainMenuActivity, i.e. A -> B -> MainMenuActivity, then after pressing back button Logout activity does its work, finishes, but instead of closing the application, activity B comes to front. I tried to launch MainMenuActivity from activity B with FLAG_ACTIVITY_CLEAR_TOP and then call finish() on B, but in that case A came to front. I also tried to set:
<activity android:name="abc.def.LogoutActivity" android:clearTaskOnLaunch="true"></activity>
But the result was the same as before. Could you please help me what should I do?
OK, so I finally managed it. After creating an activity, I register it in static ArrayList<Activity>. After "LogoutActivity" does its work; it just calls finish() on each Activity registered in ArrayList. It works, but I guess it's not really nice. But I haven't figured out how to do this with FLAG_ACTIVITY_CLEAR_TOP.
As #sandrstar says in the comment:
from API 11 you can use FLAG_ACTIVITY_CLEAR_TASK
It works very well for me, as it removes all Activities, not just the ones on top.
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();
}
}