how to continue from the activity that I quit? - android

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.

Related

Start application with second activity

In my android app, When user have successfully login with Facebook and Google Plus then user have one confirmation Activity which have next button in disable mode. While, admin will be enable user from database. If user will be enable from database by admin then confirmation Activity and next button will be enable and user can move to next Activity. When user open app in second time then user will be able to show the confirmation Activity.
I don't know, how to apply this logic :
Scenario:
First Scene:
Splash screen->Login->success->confirmation activity
Second Scene:
ConfirmationActivty->Next button enable
Assuming SplashActivity.java is your main activity
Change your code as shown below
public class SplashActivity extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor prefEditor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = getSharedPreferences("MyPreference", MODE_PRIVATE);
// returns true if value does not exist
// if it's users first time this will return true
if( preferences.getBoolean ("isFirstTime", true)){
prefEditor = preferences.edit();
// changes the value
prefEditor.putBoolean("isFirstTime", false).commit();
}else{
// add your code to open your Confirmation Activity
finish();
return;
}
// rest of your code including `setContentView()`
}
}
You can use SharedPreferences, or File or Database for user's history data and check if the user fulfill your criteria. Then you can let him move from one activity to another. Check these links
SharedPreferences Totorial
SQLite & Content Providers
You can set the visibility of "next" button to "GONE" and make a network call in the background to your server to check if the admin has approved or not. If approved, you can set the visibility of the button to "VISIBLE". And during the network call you can start a progress dialog.Click here to see how to use visibility.
[Update]
you can use something like:
final SharedPreferences prefs = getApplicationContext().getSharedPreferences(
Constants.LAUNCH_TIME_PREFERENCE_FILE, Context.MODE_PRIVATE);
editor = prefs.edit();
profile = getApplicationContext().getSharedPreferences(Constants.PROFILE_PREFERENCE_FILE,Context.MODE_PRIVATE);
if (!prefs.getBoolean(Constants.FIRST_TIME,false)) {
// <---- run your one time code here
Intent intent = new Intent(this,first_screen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
For First Scene :
You should set flag i.e isUserLoggedIn in preferences which will false by default.
After user logged in, set this flag to true.
In LoginActivity onCreate(), check that flag from preferences, if it is true finish the activity and start confirmationActivity else do nothing.
For Second Scene :
Here, you can also maintain the flag isUserConfirmed same as for login. And you can send notification from server to the user, after receiving notification update the flag to true. And enable the button on ConfirmationActivity if the activity is in the foreground.

How do remember that user signed up and take straight to another Activity?

The scenario is if the user downloads the app for first time, I ask them basic questions (say Activity A) and request them to sign up (Activity B).
This is a one time process only after installing the app. After that whenever they open the app, I am planning to take them straight away in to the app (Activity C).
How should I do this? I am a newbie to Android programming. But I am not able to think about this scenario. I don't want to use database.
You need a persistent storage mechanism to save the state of the user (logged in or not). There are various ways you can do this. The easiest is SharedPreference which will store the user state locally. You can also store this information in your remote server and validate user each time she opens the app although this might be going a bit overboard in most cases.
Try using SharedPreferences. In ActivityA in on create check if SharedPreferences contain a certain value which decides if the user is signed up. If it not set or it does not have the required value, redirect the user to ActivityB or else ActivityC
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = this.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if(pref.contains("MY_KEY") && pref.getBoolean("MY_KEY", false)){ //first stratup or user has not signed in yet
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
} else { //already signed up
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
setContentView(R.layout.activity_main);
}
Dont forget to save/insert value inside SharedPreferences after the user sign up.
Code:
SharedPreferences pref = this.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
if(sign_up_success){
editor.putBoolean("MY_KEY", true);
editor.commit();
}
The basic flow should be as follows Splash Screen -> OnBoarding Screen(Signup) -> MainActivity On the Splash Screen you need to check for a prefs value lets call it isSignedUp which is false if the user has not signed up. Its pretty straightforward from here. Every time you launch your app you need to check if the preference if isSignedUp is true or false and based on the value show the screen accordingly. So if the value is true that means the user has signed up and show him the main screen else show him the sing up screen.
Use Android SharedPreferences, it can be used to store data which can be used to keep the answers in cache.

How to change activity, when app is in the background

I have an application, that should run some loading (I/O operations) before switching to other activity, that will display loaded information. I would like to load information completly, before switching and this causes some design troubles. I might could have used AsyncTask to run stuff in the background, but from the design perspective I dont want the whole context of the Application be leaked in order to respond to the results. The concept of Service looks more likely to fit my needs.
So let me denote the situation I can't solve from design perspective. Application starts, which causes some Activity to be created and so on. This one creates a service, which does I/O operations in the background. Upon finishing it, Application changes the Activity to the specified one. The question is, how should I handle the situation, when service finishes it's job while the app is in background. As a programmer, I would like to make my app either reopen the first activity and insta change to the following one or simply start with the "following" one. Any ideas?
I would like to make my app either reopen the first activity and insta change to the following one
You could this. When processing in your service finishes save a value in SharedPreference like this
Context ctx = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("LoadNewActivity", true); // or false
editor.commit();
Now do this in your launcher activity onCreate()
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
boolean loadNewActivity = prefs.getBoolean("LoadNewActivity", false);
if(loadNewActivity)
{
Intent intent = new Intent(this, MyNewActivity.class);
startActivity(intent);
}
else
{
// Do normal startup
}

Android splashscreen backstack

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
}

how can I exit from the app without clicking on exit

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.

Categories

Resources