Having issues exiting app on double press - java

So I've implemented a double press to exit on my mainactivity which exits fine if I go to settings activity or to game activity. However, if I go mainactivity --> gameactivity --> gameoveractivity and then press back it starts the mainactivity fine but then double pressing just returns to gameoveractivity and I'm not sure why as I call finish when starting mainactivity from gameoveractivity.
mainscreenactivity:
// button listeners
playButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent gameActivityIntent = new Intent(MainScreenActivity.this, GameActivity.class);
startActivity(gameActivityIntent);
overridePendingTransition(R.anim.righttocenter, R.anim.centertoleft);
finish();
}
});
settingsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent settingsActivityIntent = new Intent(MainScreenActivity.this, SettingsActivity.class);
MainScreenActivity.this.startActivity(settingsActivityIntent);
overridePendingTransition(R.anim.righttocenter, R.anim.centertoleft);
}
});
gameactivity:
// handle hardware back button
#Override
public void onBackPressed() {
Intent mainScreenActivityIntent = new Intent(GameActivity.this, MainScreenActivity.class);
startActivity(mainScreenActivityIntent);
overridePendingTransition(R.anim.lefttocenter, R.anim.centertoright);
finish();
}
logic code to handle going to gameoveractivity:
if (weight.getBounds().intersect(player.getBounds())) {
timer.cancel();
gameTimer.cancel();
player.setTouched(false);
save(score, time);
Intent gameOverIntent = new Intent(this.getContext(), GameOverActivity.class);
this.getContext().startActivity(gameOverIntent);
((Activity) this.getContext()).finish();
}
gameoveractivity:
// handle hardware back button
#Override
public void onBackPressed() {
Intent mainScreenActivityIntent = new Intent(GameOverActivity.this, MainScreenActivity.class);
mainScreenActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
GameOverActivity.this.startActivity(mainScreenActivityIntent);
overridePendingTransition(R.anim.lefttocenter, R.anim.centertoright);
finish();
}
Edit: Managed to fix this by adding this: gameOverIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in the game logic code after creating the gameoveractivityintent.

Managed to fix this by adding this:
gameOverIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in the game logic code after creating the gameoveractivityintent.

Related

Android Studio back button activity

I've been trying to find out the back navigation button to lead to another activity.
Every time when I pressed the back button, it goes to the previous activity which is not what I want. I would like to set the back button that goes to another activity I want, instead of previous one.
For example, I have Activity 1, 2 and 3. I was in Activity 2 and just moved to Activity 3. But when I press the back button, it goes automatically to the previous activity which is Activity 2. I want to make it to Activity 1 and not Activity 2. Can anyone suggest me a solution please?
You can make the button to go to a specific activity, instead of having the default behavior that you described.
It can be something like this:
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity2.this, Activity3.class);
intent.putExtra("variable", information); //this is optional, but can be useful if you need to send a specific info to the next activity
startActivity(intent);
}
Activity 2 is parliamonar, and Activity 3 is federalparliamentary. I replaced parliamonar with Activity 1, but it still didn't solve the problem.
public class federalparliamentary extends AppCompatActivity {
Button federal;
private Object parliamonar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_federalparliamentary);
federal = findViewById(R.id.back160);
federal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reserve = new Intent(getApplicationContext(), parliamonar.class);
startActivity(reserve);
}
});
federal = findViewById(R.id.next164);
federal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reserve = new Intent(getApplicationContext(), sar.class);
startActivity(reserve);
}
});
}
public void onClick(View V) {
Intent back = new Intent((Context) parliamonar, federalparliamentary.class);
startActivity(back);
}
}
public class federalparliamentary extends AppCompatActivity {
Button federal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_federalparliamentary);
federal = findViewById(R.id.back160);
federal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reserve = new Intent(getApplicationContext(), parliamonar.class);
startActivity(reserve);
}
});
federal = findViewById(R.id.next164);
federal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reserve = new Intent(getApplicationContext(), sar.class);
startActivity(reserve);
}
});
}
public void Onclick(View v) {
Intent intent = new Intent(federalparliamentary.this, politicalsystem.class);
startActivity(intent);
}
}
Activity 1 is "politicalsystem".
I added with #Override method, but it says that I have to remove the method, so I added outside, then it says that I have to extract interface, so clicked on it, then it gave me a bunch of list. So I chose onClick(v:View ):void, but it still didn't solve the issue. I tried in another way without #Override, but nothing changed when I tested my app. I also tried inside onCreate method which did not modified the navigation as I desired.

splash screen activity appears when exiting app

Whenever I try to exit from my app by pressing back button twice, splash screen appears and it freezes until i press back button again. So I need to press back button three times to exit from my app. Please help me to exit from app with only two back button press.
The java code in project as follows:
public void onBackPressed()
{
if (doubleBackToExitPressedOnce)
{
super.onBackPressed();
MapsActivity.this.finish();
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click Back again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
my splash screen code as follows:
public class SplashScreen extends AppCompatActivity {
ImageView logoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Animation anim1 = AnimationUtils.loadAnimation(this,R.anim.anim_down);
logoView = findViewById(R.id.logoview);
logoView.setAnimation(anim1);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
},2500);
}
}
you should to finish splashActivity after start new activity, so use :
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
SplashScreen.this.finish();
}
},2500);
Add finish(); after below code.
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
You must finish() splash screen like as below than your condition on BackPress work
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();

Define source of a click?

Is it possible to define the source of a click? I can access my MainActivity through either clicking on a RecyclerView or through a Notification action. Depending on which it is, I need to provide different info. Is there a way of saying: if click is from recyclerview then..., else if it is from notification action then...?
What I can think of so far is this, but the problem is I am not using buttons as such:
Button mClickButton1 = (Button)findViewById(R.id.clickButton1);
mClickButton1.setOnClickListener(this);
Button mClickButton2 = (Button)findViewById(R.id.clickButton2);
mClickButton2.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.clickButton1: {
// do something for button 1 click
break;
}
case R.id.clickButton2: {
// do something for button 2 click
break;
}
}
}
Thanks!
you have to define two different calling intents for the same activity and put info for each View Example :
mClickButton1.setOnClickListener(new onClickListener(){
public void onClick(View v) {
Intent view1_int = new Intent (this, MainActivity.class);
view1_int.putExtra("Calling Intent" ,"RecyclerView");
startaActivityForResult(view1_int);
}
});
mClickButton2.setOnClickListener(new onClickListener(){
public void onClick(View v) {
Intent view2_int = new Intent (this, MainActivity.class);
view1_int.putExtra("Calling Intent" ,"Notification action");
startaActivityForResult(view1_int);
}
});
and in the onCreate Method in your MainActivity you can say :
String callin_view;
callin_view =getresources.getIntent.getExtras("Calling_Intent");
This will retrieve the name of the calling source you defined

On click - starting another activity after a time

I need this button, to wait with proccesing to another activity (secondclas), for the time of the animation (out1). How to code it?
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
image1.startAnimation(out1);
Intent Intent = new Intent(view.getContext(), secondclass.class);
startActivityForResult(Intent, 0);
}
}) ;
Take a look at Animation.AnimationListener(). You can implement the onAnimationEnd(...) method and launch the new activity there.
You can use an AnimationListener like this:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
out1.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
Intent Intent = new Intent(view.getContext(), secondclass.class);
startActivityForResult(Intent, 0);
}
});
image1.startAnimation(out1);
}
}) ;
You can get the animation time with out1.getDuration(). The intent can be started after that time using a delayed handler.
More details about how to start the delayed handler can be found here: wait t time before launch an action?

Press back button to another activity

I try the code below to call another activity while pressing the back button:
#Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {
switch(keyCode) {
case(KeyEvent.KEYCODE_BACK):
Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window w = NASGroup.group.getLocalActivityManager().startActivity("BActivity", intent);
View view = w.getDecorView();
MyGroup.group.setContentView(view);
return true;
}
return false;
}
But when I press the back button, it get out of the app.
I see the logcat, it does not run the function onKeyUp and doesn't output any message.
The same code in onKeyUp, I try to below code to a button in layout and it works.
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window w = NASGroup.group.getLocalActivityManager().startActivity("BActivity", intent);
View view = w.getDecorView();
MyGroup.group.setContentView(view);
}
});
How can I modify it?
To handle back press you have to override Onbackpress method.
#Override
public void onBackPressed() {
finish();
Intent intent = new Intent(Myactivity.this, other.class);
startActivity(intent);
}
Try overriding the activity's onBackPressed() method
from the docs : onBackPressed
Try like this...
#Override
public void onBackPressed() {
Intent BackpressedIntent = new Intent();
BackpressedIntent .setClass(getApplicationContext(),TargetActivity.class);
BackpressedIntent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(BackpressedIntent );
finish();
}
For back button you have to override the OnBackPressed() in your activity as
#Override
public void onBackPressed() {
Intent intent=new Intent(currentclass.this,nextActivity.class);
startActivity(intent);
finish();
}
if you didnt finish the previous activity then no need to use intent and startActivity just call finish(); in the onBackPressed it will finish the current activity and previous activity will started.
If you are within Activity you can use onBackPressed which is a built-in method to handle back key press.

Categories