I am developing an application where one of my Activity contains a button as "Set as Home Page".
So my problem is that when I will click this button the status will be saved in the shared preference and next time when this application will be opened I want to start this Activity (the Activity which has been set as home page) instead of the default one.
So how can i do it???
You need to have defined static constant unique ID for each of your Activity. You save this ID to shared preferences, and implement on your boot activity's onCreate event a switch based on this stored ID against the static constant ID of your Activity. When you have the right step start the activity, and finish your current activity the booter.
You could create a kind of redirection Activity on which your application would start on. Then, put a switch in this activity, with intents sending to each of your activities, and the state of the preference would be the variable to test for the switch.
I'm not sure I'm clear but tell me if it's fine for you?
Related
My Android app has a class called ListQuotes, which, as implied by the name, displays a list of quotes. It can be opened in three instances :
by selecting an individual from a list to see his quotes
by clicking the "Favourites quotes" button on the toolbar
by selecting a category from a menu ("quotes about love", "quotes about friendship", etc...)
Everytime the class is launched, the app checks the Intent to determine where it came from and to display the appropriate list.
I set up its launch mode as singleTop so that the app doesn't try to reload the class when I press the back button and that the intent is preserved.
However, by doing so, I cannot load the favourites from the toolbar if ListQuotes is already the foreground activity. In other words, if I select an author to display his quotes, and then click on the favourites button, it doesn't do anything.
Is there a way to make it so that the launch mode of an activity stays in singleTop, but that you force the app to open a new instance of the activity in a specific case ? Or, perhaps, to have the launch mode as standard, but have a way to recover a previous intent? Or should I just make a separate activity for my favourites ?
I hope this was clear, I haven't programmed in a long while, so it's possible I'm missing something. Thanks in advance.
Remove the singleTop launch mode. This will allow you to create another instance. Your statement:
I set up its launch mode as singleTop so that the app doesn't try to
reload the class when I press the back button and that the intent is
preserved.
makes no sense, as pressing BACK will not cause this strangeness.
My app begins with Activity A (log in screen) which I always want on the bottom of the Activity stack.
Based on some logic after login, I launch some other Activity B-Z. There is a menu in the app which allows you to switch around between any Activity B-Z.
If the user hits the back button enough times, I don't want them returned to the login screen. I want the user to be sent to the Android Home screen if the back button is pressed on the Activity which has Activity A as the next Activity on the stack. This Activity is not guaranteed to be the Activity which was launched by Activity A because my Activities use singleTop.
Ideas?
The only other option I can think of is to remove singleTop and whatever Activity is launched by Activity A could remember that (my Activities all derive from a base class, which I would use to do that).
Another possibility may be to do something like the following in the onBackPressed handler:
if (getParent().getClass().getName().equals(ActivityA.class.getName())) {}
Although not a direct answer to your question, but if the problem is that
I don't want them returned to the login screen
then the classic solution is to finish() the login Activity when the user has logged in successfully. This way you'll make sure the user will never return to that Activity.
To do this, Why don't you get the User Login information, and store it in your apps private shared preferences. Then when the User launches your application, you can read the Shared preferences from activity A, and automagically log them in to activity b .
something like this:
SharedPreferences myPrefs = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
if((myPrefs.getString("username",null) != null) && (myPrefs.getString("password",null) !=null)){
// Make sure your user is a member of your application
// auto log in to the home page
Intent bIntent = new Intent(getBaseContext(), BIntent.class);
startActivity(bIntent);
}
For more reference on how to use Shared Preferences
http://developer.android.com/reference/android/content/SharedPreferences.html
Then if you want to be really slick, Store the Class name when the onDestroy() method is called in each Activity, overwriting each class as the last open Activity. So whichever activity the user was last on before the application was closed is stored in preferences and you can read that in the login activity, then from there launch b-z
This will make it so that the login activity is always in memory, since it is first launched to check your users credentials.
I have an activity to set my preferences the first time that application get opened, but how can I change activity status (move the "MAIN" on another activity)
You cannot move the MAIN Activity to another activity at runtime. It is mandatory to define it while writing Manifest file. If you want to do that as per preferences values then check the preference value everytime and then move to your required activity.
Open SplashScreenActivity--> check preference value here-->If false go to your FirstActivity when app launched newly-->If true go to your AnotherActivity.
Hope you got the concept.
Suppose the user is in the activity A of my application.
The user leave the application (with the home button) and at some point, while he is doing something else, I want to change the stack of my application to A B. I do not want the activity B to pop up from nowhere, I just want that when (if) the user returns to my application, he sees the activity B.
It seems that calling startActivity(B) in the activity A from a background thread works, but I’m not sure this will have the desired behaviour on every platform (what I want is that the user does not see the activity B until he returns to my application)
I may not be following this correctly
If all you are after is saving state (which activity was last active) you could use preferences Shared Preferences
You could then just have a MainActivity that does nothing other than decide which activty should be displayed the next time your application starts
Main
Using preferences find the last activity that was active
Start first activity dependent last activity ID or whatever logic you choose
Activity A or B starts/resumes etc - Store activity ID in shared preferences for retrieval later
How to change main activity at application startup in Android?
I want to implement lock screen asking for password, before allow user to interact with application. Password screen can be turned of in application's settings and in such case (when application should not ask for password) I want to start Main application activity not Password activity. How it is possible?
In the onCreate of the Main activity, check preference and if password is on startActivity to load the Password activity.
First, I am new to Android programming so bear with me!
If I was doing it, I would have created another activity that would be the main activity (set in the AndroidManifest.xml file) and inside the onCreate() event I would have checked that parameter in your setting file and depending on its value, I would have launched the password activity or the other one.