Reputation: 123
I would like to make a particular layout visible during the startup of an application, not when the activity startup every time the user is accessing the activity in the app, can someone tell me how to do that?
I have tried onstart() method and read so many answers but most answers are about background and foreground, they aren't what I want, onstart() keeps showing the layout every single time the activity was accessed.
I only wanted the layout to show one time when a user access the app from the phone (when app has been killed in background or when it is not previously in the background before/opened for the first time on that day for example). A one time thing, but not when the first time the app is used but every time the user enter the app (after the app is not in / deleted from background).
Please help, thank you in advance.
Upvotes: 0
Views: 95
Reputation: 337
I think the simple solution is just create the layout in same layout(Which you are using with activity)(both layout should covers full screen you can achieve this by relative layout) and set in XML visibility="Gone" for the layout you want to show for few seconds, now when activity start set the id.setVisibility=VIEW.VISIBLE for layout you want to show for few sec and set count-down for 3/4 sec and on countdown finish set id.setVisibility= VIEW.GONE,, You have to use Relative Layout for this purpose, and another solution is you can use splash screen if your activity is Launcher Activity.
Upvotes: 1
Reputation: 329
Use SharedPrefences, like this:
public class MainActivity extends Activity {
SharedPreferences sp;
Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
sp = getSharedPreferences("mypreference",
Context.MODE_PRIVATE);
editor = sp.edit();
}
@Override
protected void onStart() {
super.onStart();
//we set default value to true because if it's not initialized yet, it is first time!
if(sp.getBolean("key_first_time" , true)){
//it's app first time start up
....
//change status of shared preference to false
editor.putBoolean("key_first_time" , false);
editor.apply();
}else{
//it's not app first time start up
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//this method called when your activity is not in memory anymore
//change status of shared preference to true
editor.putBoolean("key_first_time" , true);
editor.apply();
}
}
Note that when activity removed from android memory(background) our sharedprefrence will change to true, in other word our first time status will reset.
Upvotes: 1