I have to exit from the app without clicking exit button.. I have 3 activities. One is main, another one is register and the other is login. I am in the login page so that if I press back in the mobile , I should go directly to the home.. Help me friends !! Thankyou..
Use the below code to finish current activity when moving to another activity this will ensure that no activity runs currently except current.
Intent intent=new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
CurrentActivity.this.finish();
So now when you click back button it will not display the T&C page and will exit. But if you want to show any other page instead of exit then override onBackPressed() method.
U want once u saved the details after that u want to open a new activity i hope .See this i have also done the same thing it will help u
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
if(shared.contains("username") && shared.contains("password")){
startingActivity();
} else {
//What u want to do here
}
I think the problem, as I understand, is that you are only calling finish() if the SharedPreference value is false. But you want to close this page no matter what or else it is still in the stack. So move finish() out of the if and close this Activity no matter what the value is.
SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean haveWeShownPreferences = prefs.getBoolean("HaveShownPrefs", false);
if (!haveWeShownPreferences) {
Intent y=new Intent(this,Registerwithdialus.class);
this.startActivity(y);
}
finish();
Let me know if I don't understand the problem.
Related
I'm Creating A game so I have several Sharedpreference for the score and 2 for check if I was on this activity to not repeat it. So when i quit the app(restart) and reopen it I Want when pressing the start button and it continues from A2 or A3 or ... only if i exited from this activities but my sharedPreference not allowing it Because it's job is to not repeat the activity if the user enter it once. This is my 2 sharedpreference please someone guide me what to do...
SharedPreferences pref = getSharedPreferences("a", Context.MODE_PRIVATE);
if (pref.getBoolean("aa", false)) {
Intent intent = new Intent(this, A2.class);
startActivity(intent);
finish();
} else {
SharedPreferences pref1 = getSharedPreferences("a", Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref1.edit();
edt.putBoolean("aa", true);
edt.commit();
}
I just want when i exit the app or pause it to continue from where the user Stops
** i don't want to repeat the same activity if the user already answered the question but if he exits without answering I want to continue from where he left**
Thanks
Put a return after your finish statement in the IF loop. Please remember finish does not alter the control flow but it merely asks the system to finish your activity. So if you don't want rest of your code to execute, put in a return statement. I guess rest of your code is fine.
How can prevent when user click back, only back to one page before the login Activity. If hit the last page before login Activity, then exit the app. Now, if i click go back it will show the login page and need login again. Once user login, unless they click logout button, otherwise don't show login Activity.
Any idea on this?
Thank you.
If you would like to simply prevent the back button to navigate back to your login Activity, you could just set the android:noHistory attribute to true for your login Activity in your Manifest.
Something like this:
<activity
android:name=".LoginActivity"
android:noHistory="true" />
Always open your activity after splash screen. Decide which activity you want to navigate the user using the stored login data. To store login data use shared preferences. But at the time of login, after logging in if user clicks back, user will be navigated back to login activity. So once the user successfully logged in clear the activities on stack while navigating the user to main/home activity
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
use shared preference to achieve this:
On Login successful, write a shared preference like below
SharedPreferences settings = getSharedPreferences("your_preference_name", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("LoggedIn", true);
editor.commit();
Also to prevent other activities to come back to Login page, we should finish the login page before moving to target screen
Intent intent = new Intent();
intent.setClass(this, TargetClass.class);
startActivity(intent);
this.finish();
Also whenever u come back to the application, u can directly move ur user to Home page by this
SharedPreferences settings = getSharedPreferences("your_preference_name", 0);
boolean isLoggedIn = settings.getBoolean("LoggedIn", false);
if(isLoggedIn )
{
//Go directly to Homescreen.
}
when you start an activity from Login Activity then after StartActicity(), finish the login activity by calling finish() function.
call finish() in your login activity when opening the next activity after user logs in. And as someone pointed out that you can store your authtoken in sharedPreference once user logs in and use that data to maintain the session.
You can do it by this way too, via playing with the launchmode of the android manifest.
<activity
android:name=".yourPackageName.LoginActivity"
android:launchMode="singleTop"/>
when you do login and Intent than call in login Activity
finish();
In Kotlin, this is how to go about it:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
i have two main Activity with two separate XML file that design for two languages.
when someone tap on Image button on Main Activity ENG ,it will change and another activity with different layout and language shows up.
the point is, i have on Back Pressed() method that if user pressed back button twice , it should exit the app.
now what i get is when tap a lot and switch between two activity the back pressed button wants to back to previous activity to the end that it seems absolutely right but i need to quit directly. what can i do?
public void onBackPressed()
{
if(count == 1)
{
count=0;
finish();
}
else
{
Toast.makeText(getApplicationContext(), "Press Back again to quit.", Toast.LENGTH_SHORT).show();
count++;
super.onBackPressed();
}
return;
}
thanx
Simple scenario, You start Your Activity B from Activity A by button press:
yourButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(YourActivity.this,YourNextActivity.class);
startActivity(i);
finish();//finish Your first activity
}
});
The only thing You have to do is, to finish the first Activity from where You get to the next one. Then Activity B has no chance to go back to Activity A and the app "finishs".
This is an article about exit and quit in android apps.
http://android.nextapp.com/site/fx/doc/exit
but for your app purposes you code use these lines:
finish();
moveTaskToBack(true);
System.exit(0);
This is the correct behaviour, to quit, you'd have to call finish() on both activities. However, if you create the second Activity with the Intent.FLAG_ACTIVITY_CLEAR_TOP flag, you'll be able to quit as described. However, your users won't be able to go back in the activity hierarchy - are you sure you want that?
Anyways, this way you'll be able to quit:
final Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I am implementing a splashscreen for a android application. I only want to show the splashcreen once when the app is newly started. After i've done some work I want to go on to the app. If the user then presses the back button I don't want it to return to the splashscreen, I just want the app to exit. How can I implement this in the best way? How do I clear the backstack of the first activity.
If you want to show splash screen only for first time when app launches,
then you can use above solution of share preferences.
But i guess you want to go through following scenarios:
You start the app you get splash screen.
Then you navigate though the app.
Then you come to home screen of you app.
Then you want to exit but splash screen comes.
If you are having this problem then you need to finish splash screen
when you start home activity and also at the end you need to logout or finish the app home activity.
Also try android:launchMode="singleTask" in splash screen activity tab in android manifest.
Use shared preeferences in android and store the value 1st time.. from second time check if value present dont display splash screen
Edit for preferences follow this
When you are going to the second activity after the splash screen, call finish()
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (preferences.getBoolean("firstTime", true)) {
// Show splash screen
// Wait a few seconds.
} else {
// Nothing to do here. Go straight to the second activity.
}
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(
getSupportActivity()).edit();
editor.putBoolean("firstTime", false);
editor.commit();
startActivity(MainActivity.this, ...)
finish();
This way when the user presses back, there wont be any activity in the stack.
Hi, try this code it will help you but it will show the splash whenever you open the app.
public class spash_scr extends Activity {
ImageView t;
//LoginButton b;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spash_scr);
// bm1=drawable.shineme;
t = (ImageView) findViewById(R.id.textView1);
t.setImageResource(R.drawable.shineme);
RotateAnimation r = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// r.setStartOffset(1000);
r.setDuration(2000);
r.setFillAfter(true);
t.startAnimation(r);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(spash_scr.this, MainActivity.class);
startActivity(i);
finish();
}
}, 3000);
}
You weren't clear in the exact behavior, so here are some options:
A: You want it to show the splash activity every time the task (app) is restarted, such as after phone reboots, user's manual task closing, or when Android drops it for memory reasons. (This is usually used for branding or licensing logos.) In those cases, launch the splash from the main activity's onCreate(), then Finish() the splash screen to allow the user to return to the main view. That way navigating back won't bring the splash activity back, since it is not in the navigation stack anymore.
B: You want it to show the splash screen the first time the app is launched after installation, but never again. (Usually used for 'welcome' or 'get started' help views.) Use a SharedPreference setting as described in other answers here, or in the Using Shared Preferences documentation. In the case it should show the splash, I still suggest option A for the simplest way to not show the splash screen again for after it is first dismissed after the first launch.
C: An even more, unknown complex navigation? Learn about Tasks and Back Stack and you can make it do pretty much whatever you want.
After opening the new Activity this.finish (); You should do
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
this.finish ();
For that you should use SharedPreferences ,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
Intent i = new Intent(splash.this, otherSplash.class);
this.startActivity(i);
this.finish();
}
else
// Not firsttime Direct it as you wish
}
I have a question regarding android activity.
I have my first loginscreen(MainActivity) and from there go to Secondscreen, now when I am pressing escape button from the secondscreen I want to close the application(or android home screen) and next time open the application I want to start application with secondscreen.
i tried this code on my second screen but it does not working
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE)
{
//My logic here
return true;
}
return false;
}
First time when you use the application, Start with LoginScreen,
then call finish(). Also store a shared preference to know that, the
application is used atleast once(Logged in)
Since you finished the LoginScreen, when you press back button on SecondScreen, you will come to device home, not the LoginScreen.
Next time when you want to Start the application, check the sharedPreference whether the user is logged in, and finish the LoginScreen just at the start of onCreate
LoginScreenActivity:
onCreate(){
//check shared preference if logged in
if(yes){
startActivity(new Intent(Login.this, Second.class));
finish();
}else{
//do all the login here then,
//set sharedPreference
startActivity(new Intent(Login.this, Second.class));
finish();
}
}
What I assume you are trying something like below:
Frist installation#
LoginScreen>SecondScreen, when you press escape it will close entire app.
Second Time#
When you open app it will start from second screen instead loginScreen.
Solution
Phase1#
Store your login info in database or sharepreference when first time login has been made, and finish() loginscreen while opening second screen.
Phase2#
When you open second time your app, check your login info, if it is exists then redirect to second activity else open loginScreen.
When you start your second activity at that time finish the existing acitivity with finish()
example
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
This code will finish your current acitivity and when your press back button in second activity then the application gose close.
In log in activity call finish after starting second activity.
startActivity(i);
finish();
Now if u press back it should close the app and when u going to start app again the second activity will be visible.
There are two ways to do that:
make your log in activity with "noHistory=true" in android menifest activity definition.
<activity android:name="YOUR_FIRST_ACTIVITY"
android:label="FIRST_ACTIVITY"
android:noHistory="true" />
You can finish() the first activity at the time of starting second activity.
startActivity(SECOND_ACTIVITY_INTENT);
FIRST_ACTIVITY.this.finish();
Hope it will help you.