I am developing an android application. So in there I have button called "Aboutus". when I click that one it starts another activity and show corresponding view.
Here is the code for button Aboutus click event.
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,AboutUs.class);
startActivity(intent);
}
});
}
And then in that aboutus activity I have back button.when we press that back button it will go to the main activity again.
back button key event goes like this..
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
startActivity(intent);
finish();
System.exit(0);
}
});
here i put some intent extras for some other purposes.In this case they do not matter. So and then again in mainactivity i have exit button. which should kill whole app.
and my exit button code goes like this.
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
System.exit(0);
}
});
Exit button works perfectly (it ends the app) except for one scenario. if we click on aboutus and go to that activity and then press back button on that aboutus activity and then again come back to main activity and finally when i want to exit it won't kill whole app, instead it again goes to the about activity.
in conclusion,
MainActivity---> click aboutus button(no problem in here.this will start aboutus activity)
Aboutus----->click back button(this also works fine. go back to main activity)
MainActivity--> Exit button (not working .it goes to aboutus activity again)
So how to fix this problem?
i dont see why you have to start MainActivity.java ...the user can just hit the back button as long as its on the activity stack. Also The system.exit(0) is bad. Just let the app finish gracefully with finish().
if you want to pass something to the activity when aboutUS is done, you can look up onNewIntent() method.
use intent flag ACTIVITY_CLEAR_TOP to clear all activities on top of it:
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP From the doc:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
Related
There is timer in GameActivity. After time finishes, end of round activity runs.
When I press back button in GameActivity, previous Activity (MainActivity) runs. But in background, GameActivity is still running. After time finishes, I see end of round activity screen although I am in MainActivity.
That means game activity runs in background. How can I stop GameActivity when I press back button?
I tried Finish() method.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(GameActivity.this,GameSettingsActivity.class);
startActivity(i);
finish();
}
You should stop your timer in the GameActivity's onPause() method. This will ensure your timer is stopped when hitting the back button but also when your app goes in background.
#Override
protected void onPause() {
super.onPause();
//stop your timer here...
}
I have an activity which is repeated "x" times with this piece of code :
//create intent to start itself again with different parameters
Intent intent = getIntent();
intent.putExtra(CATEGORY_ID, temp.getCat_ID());
intent.putExtra(CATEGORY_SHOPID, temp.getShopID());
intent.putExtra(CATEGORY_SITEID, temp.getSiteID());
intent.putExtra(CATEGORY_NAME, temp.getCat_Name());
finish();
startActivity(intent);
My goal is that when the user presses the back button that the same activity gets started as there was before with the same parameters as before and such. I tried saving them in an Activity ArrayList but that doesn't seem to work.
When you call finish, your activity will end.
When you don't call finish, your activity just go to the background. In that case, when you press the backbutton in your new activity, your previous activity comes up.
I think when you just delete finish(), it will work.
It's okay if you save the parameters you've already set earlier, but you need to retrieve all that information from the Intent itself and repopulate your activity with the proper data.
This must be done like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String categoryId = getIntent().getStringExtra(CATEGORY_ID);
// retrieve the others and populate your activity
}
in your activity, get event back
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// and you have to make a intent to call this activity again with parameter other
finish(); // and call finish() here to close the old activity
}
return super.onKeyDown(keyCode, event);
}
hope this help!
Have you tried overriding the onBackPressed method?
I have this app where when it navigates to another activity it does a transition animation. Now I have already got that working properly but, to get it to work i have to turn the animation setting on my phone on. I have been searching on Google and I can"t seem to find solution to my problem. So my problem is this,is it possible to turn this setting on from within the app, with a code, instead of having to manually do it?
Here is the code but as i have said above this works perfectly and I'm using a Telstra next G Huawei phone, just want to no if i can code it so i can turn the animation setting on if i decide to put it on a different phone.
//Button to restart from beginning:
//Declaring the button for OnClickListener and EditText to send data:
private void Restart() {
Button Next = (Button)findViewById(R.id.restart3);
Next.setOnClickListener(new View.OnClickListener() {
//The action the button takes when clicked:
#Override
public void onClick(View v) {
//Goes back to the start of app:
Intent i = new Intent(Height.this, Page_1.class);
//clears the stack of all Activities that were open before this one so that they don't stack up:
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
//The transition animation when going onto next page:
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
// to end the current activity:
finish();
}
});
}
In my application, I have two activities. First is a splash screen, which simply shows the application name and few other info.
Upon clicking on the splash screen activity, I'm loading the main activity. My app works fine, but I'm facing a small issue. If I press back button from my main activity, control is going to splash screen activity. But I don't want to show the splash screen activity again, I want to avoid splash screen activity when pressing Back button.
Is it possible? If so how?
In your AndroidManifest.xml file, add android:noHistory="true" attribute in your splash screen <activity>.
As I understand, you want the splash activity to not show after changing activity. You should note activities save On Stack and with starting new activity push on it and with finish you pop on top stack. I think that if you the call finish() method your problem fix as in your splash screen activity where you call StartActivity insert finish() after
public void onClick(View v) {
Intent intent = new Intent(Main.this, Splash.class);
startActivity(intent);
finish();
}
Hope to be useful :)
You can just call
finish();
In your Splash screen when you jump to the second screen.
In addition to the above answers, you should note
that:
1: by calling the finish() method, the Splash activity
will close after execution, meaning that it will not be
available in the stack.
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
You will achieve your aim using the method above
but...
2: If you want to prevent your users from force
exiting the app (pressing back button) while the
splash activity in still ongoing which is the best
practice in android, then you need to call the
onBackPressed () method.
Class NoBackSplash{
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
#Override
public void OnBackPressed(){
};
}
With this OnBackPressed() method, your splash activity will not be force to exit no matter how hard the user try.
I understand, you want the splash activity to not show when you click on back button. First of all you should know that all the activities on android are in form of STACK. So what we need we just end the splash activity after it execute. we can do this by calling finish() method in android studio. here is the solution:
Intent intent = new Intent(MainActivity.this, home.class);
startActivity(intent);
finish();
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
#by calling finish() method,splash activity will close after execution
finish();
}
},3000);
}}
I am using TabActivity(for example, TActivity) to create tab and in every tab I am loading instance of same activity(for example, MainActivity).
Through this activity I want to switch another activitity(for example, ChildActivity which will be same for every tab i.e. in every tab we will get instance of this activity) using Button click.
This button is hardcoded in original TabActivity(TActivity).
Now I am able to do up to this. But Now I want to switch between 'MainActivity' and 'ChildActivity' using same Button.
MainActivity:
class MainActivity ...{
onCreate(){
Btn.setOnClickListener(new OnClickListener(--) {
public void onClick(--){
Intent intent = new Intent(getBaseContext(),ChildActivity.class);
replaceContentView("MainActivity", intent);
}});
}//MainActivity
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)) .getDecorView(); this.setContentView(view);
}//replaceContentView
in ChildActivity:
public void hideCurrentActivity(){
Intent intent = new Intent(getBaseContext(),MainActivity.class);
replaceContentView("ChildActivity", intent);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) .getDecorView(); this.setContentView(view);
}
Ok, now problem is that this code creates fresh activity everytime. So all previous data is getting lost once I switch to previous activity.But i don't want to create fresh activity instead just want to bring old one to front.
Can anybody help?
Thanks in advance.