Android home screen launcher - android

First of all let me tell you guys that I am completely newbie to Android app development.
I want to know how to launch home screen and then application home screen.
i.e whenever you click on some app, first it will show some logo screen and then automatically jump to home screen of the app.
I have my home screen of the app running, I just want to add some kind of logo screen when user click on the app.
I did little bit research on this and I found out
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If I add this to logo activity then how to call app_home_screen activity automatically
Hope I am clear enough to explain the question.

You need to:
Add a new Activity SplashActivity which will be the start Activity of your app.
When your app starts, launch your SplashActivity and wait for some time.
When you finish waiting, start your MainActivity and finish the SplashActivity.
Here is a good example:
SplashScreenActivity:
public class SplashScreenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout); //You need to define it in your layouts
final int welcomeScreenDisplay = 3000; //splash lasts for 3 sec. You can change it
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
startActivity(new Intent(SplashScreenActivity.this, MainScreenActivity.class));
finish();
}}
};
welcomeThread.start();
}
}
And don't forget to add the activities to your Manifest file:
<activity
android:name=".SplashScreenActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainScreenActivity"
android:label="#string/app_name" >
</activity>

Although the other answers certainly work, here is an alternative to avoid having a separate activity just for your logo:
Place your ImageView as you would like (i.e. covering your full screen) before the other elements in your main activity layout (they will have an initial layout outside of the screen).
After calling setContentView in onCreate, run SystemClock.sleep(LOGO_TIME), where LOGO_TIME is an integer representing the number of milliseconds you want your logo to appear.
Finally, call setVisibility(View.GONE).
Your ImageView will be gone, and the other layout elements will fall into its place.

One solution you could attempt is to load the logo as your main activity, and have the application pause for a few seconds before calling the 2nd activity, your 'true' home page.
To start that 2nd activity you'll use 'Intent' such as
Intent myIntent = new Intent(myFirstActivity.this, realHomePage.class);
myFirstActivity.this.startActivity(myIntent);
Just make sure to extend the Activity class in your home page class

Have the main activity that gets called when your app icon is clicked on be an activity which has a view with only your logo on it. Something simple, just a RelativeLayout and an ImageView. After a time delay, have this activity start your actual main activity with an intent.

Related

How can i create only one time use splash screen for my android app

I want to create app in which splash screen came only one time.
like i just downloaded the application from playstore and when i pressed on application icon. only very first time splash screen showed. for rest of the time. it don't open like go straight way on second activity. How can i do this.
thank you.
You can achieve this by creating a launcher activity without an UI and SharedPreferences:
public class LauncherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("App", Context.MODE_PRIVATE);
// Get value from shared preferences,
// if null (app is run first time) the default value (second argument) is returned
boolean isFIrstRun = preferences.getBoolean("isFirstRun", true);
if (isFIrstRun) {
// set isFirstRun to false
preferences.edit().putBoolean("isFirstRun", false).apply();
// launch splash screen activity
} else {
// Launch other activity
}
}
}
And make sure LauncherActivity is set as the launcher activity in the app manifest.xml:
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Changing Main Activity in Manifest result to shortcuts doesn't work anymore

I try to add welcome tutorial for users that install application for the first time. That activity need to be declare as Main in Manifest (or I miss something?). But if I choose any other activity else than main one (which is actual app), app shortcuts (Android 7.1) doesn't work anymore. It's interesting however that shortcuts are still available at custom launchers (Apex, Nova). Any idea?
(almost all) Google apps has welcome tutorial as well as launcher shortcuts. I can't get it how they did it?
A welcome tutorial does not have to be an activity. It could be some other sort of presentation (e.g., a fragment).
A welcome tutorial, even if it is another activity, does not have to be the launcher activity. The launcher activity could detect that it is the first run and started the tutorial activity.
Thank you for the answers CommonWare! Your statements helps me to find answer. So, I want to start an app which shows Splash screen, then Welcome tutorial. Also, app need working shortcuts on main screen as well as only one launcher icon. So, first, I declare Splash screen as main in Manifest.xml:
<activity
android:name=".SplashActivity"
android:noHistory="true"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="#xml/shortcuts" />
</activity>
Then, Welcome (tutorial) activity:
<activity
android:name=".IntroActivity.WelcomeActivity"/>
After that, in SplashActivity.class checking first launch:
public static final String FIRST_APP_LAUNCH = "com.ips.test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirstAppLaunch()) {
setFirstAppLaunch(false);
startActivity(new Intent(this, WelcomeActivity.class));
} else {
startActivity(new Intent(this, MainActivity.class));
}
finish();
}
private boolean isFirstAppLaunch() {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
return preferences.getBoolean(FIRST_APP_LAUNCH, true);
}
private void setFirstAppLaunch(boolean value) {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(FIRST_APP_LAUNCH, value);
editor.apply();
}
}
Final result is as I wanted: app launch with Splash screen, then it runs Welcome tutorial. Next start will trigger Splash screen which will continue to main activity (app itself). When user click on shortcut in main screen it will get shortcuts, and in Launcher it will have just one application shortcut.

If App running, start login activity else splash activity

Update
I changed the logic as pointed out by #dunnololz in his answer. But now Splash always appears when clicking on launcher icon even though application is running. I was hoping first time when app is not running splash would show otherwise login activity show but this is not happening.
Here is my code:
manifest splash launcher activity:
<activity
android:name=".activities.Splash"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Splash.Java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(Splash.this, IMService.class));
setContentView(R.layout.activity_splash);
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3 * 1000);
Intent intent = new Intent(getBaseContext(), Login.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
I am using this gist to determine whether app is running or not. I found it via this tutorial. So that class uses Application.ActivityLifecycleCallbacks for API level 14+ which is what I need.
What I want to do is:
If application is running either in foreground or background, start login activity
If application is not running, start splash activity
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(getBaseContext(), Splash.class);
if (Foreground.get(this).isForeground()
|| Foreground.get(this).isBackground()) {
intent = new Intent(EntryPoint.this, Login.class);
}
startActivity(intent);
finish();
}
And here is my application class:
public class MyApp extends Application {
#Override
public void onCreate() {
super.onCreate();
Foreground.init(this);
}
}
Of course I have added my app to manifest file as well:
<application
android:name=".MyApp"
The problem is that Splash activity is never started. It is the Login activity which is always started.
I just want to be able to do this:
If application is running either in foreground or background, start login activity
If application is not running, start splash activity
But not sure how to get this working, I might be missing something obvious here. Or even it might be that my requirement of either in foreground or background is wrong as I am new to Android.
Thanks for the help
Generally in Android it is not advised to determine behavior based on whether the app is currently being kept in memory or not. That is what the OS should be concerned about and not something you want to concern yourself about.
In the current version of Android (since I'm not sure if this behavior is true for older versions), when you tap an app to launch it, the OS checks to see if the app is already "open". If it is, it restores the state of the "open" app. If the app is not already "open", Android will launch the activity that is marked at the launcher. So the solution to this problem is simply to let Android handle it. Make a splash activity and mark it as the launcher in your AndroidManifest.xml. Then have the launcher open your login activity after some time.
Now, if the app is already open, Android simply will restore the last open activity. Otherwise it will show the launcher.

Android onCreate called on Second Activity

Hi I have 2 activities,
Activity A and Activity B,
So its Activity A and on a Button click I go to Activity B.
Now I press home button and go and do somany other things. Because of memory issue the task is closed by android. Now when I try to open it again it starts from Activity B. Is this expected?
If yes is there a way to prevent it?
Below is the part of AndroidManifest where both activities are defined
Activity A = MenuActivity, Activity B = AndroidLauncher
<activity
android:name=".AndroidLauncher"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:finishOnTaskLaunch="true"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".MenuActivity"
android:clearTaskOnLaunch="true"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Also I am putting the code of onCreate of Activity B
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_view);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
int gameType = getIntent().getIntExtra("GAME_TYPE", 0);
GDXtoAndroidInterface.sharedInstance().activity = this;
TurnBasedHelper.sharedInstance().listener = this;
FrameLayout lg=(FrameLayout)findViewById(R.id.layout);
lg.addView(initializeForView(new LetterPress(GDXtoAndroidInterface.sharedInstance(),gameType), config));
busyDialog = new ProgressDialog(this);
busyDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
busyDialog.setCanceledOnTouchOutside(false);
busyDialog.setCancelable(false);
showBusy("Setting up..");
Log.d("MSG", "on create called launcher");
}
Also the reason why I need this is Activity A is like a login screen and Activity B is based on it. Activity B cannot work alone. User has to move from Activity A to Activity B
Thanks
when I try to open it again it starts from Activity B. Is this expected?
yes, this is the expected behavior.
when you press the home button (not the "back button") the entire task is sent by default to background, and when re-launching it (from recent tasks screen or the app icon at the home screen) it coming back by default to foreground with the same stack state as it was. same behavior applies also if your app was killed by the system while it was in background to preserve memory. the operating system will store the state of each activity in the stack, an restore it when it need to re-created (see this post)
you can think of the home button in relation to Activity like the relation in Windows OS terms as the "minimize" window button, and back button would be like the "close window" (x) button.
there a way to prevent it?
assuming you have good reason to do so and break this consistence expected behavior, you can use all kind of combinations between activity launch mode and intent flags that will give you the behavior you want..

Issues with Translucent Theme

I have an app that has two activities.
The first one is presented with a single button that opens the second one.
Here is the Manifiest definition for the first one:
<activity
android:name="com.example.buttonexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Second activity:
<activity
android:name="com.example.buttonexample.MainActivity2"
android:label="#string/title_activity_main_activity2" android:theme="#android:style/Theme.Translucent">
</activity>
Here is how I launch the second activity (via OnClickListener for a button on the first activity):
public void startSecondActivityClick(View v) {
Intent startActivity2 = new Intent(this, MainActivity2.class);
startActivity(startActivity2);
}
This works fine, however when I background the app by hitting home and the foreground the app. I'm noticing that the first activity is continually creating/destroying itself. I verified this by putting some code in the onDestory method to increment a static int:
private static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
}
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivity", String.format("Destroyed, %d", count));
}
I've also noticed that removing the translucent theme seems to fix this. My question is is there a way to translucent or something similar but also have it not restart? Also, I'm curious why this happens at all. I'm testing this on 4.0.1 ICS on a galaxy SIII.
Ok after some digging I was able to figure out why this is happening. Someone had turned on one of the developer options, "do not keep activities.". After turning this off this stopped happening. I suspect this wouldn't happen in production too often as most people probably don't have that setting on. You can find this under settings -> "developer options" on most phones.

Categories

Resources