Main Activity and Splash Screen Priority Prblm - android

I have a problem with Main Activity and Splash Screen.There is intent filter both of them
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
When I remove intent-filter from MainActivity the app cannot open.What must I do ?

AndroidManifest.xml
<activity
android:name="com.example.SplashScreen"
android:label="#string/app_name"
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>
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name" >
</activity>
SplashScreen Class
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Main Activity Class
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

You need to have intent filter only for your splash screen. And you need to start main activity from your splash screen activity.
Manifest
<activity android:name="com.example.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.MainActivity"></activity>
Start main activity from splash screen and finish it
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();

Related

How to show splash screen when starting app from shortcut

I was unable to find out how to show splash screen when starting app from shortcut. Shortcut is a direct path to open certain activity, so splash screen is bypassed. any idea how to include splash screen at every app start combinations ?
My splash implementation:
public class SplashScreen extends AppCompatActivity {
Context context = this;
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 2000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed( new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent goToMainActivity = new Intent(SplashScreen.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
Manifest:
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait">
<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/shortcut" />
</activity>
<activity
android:name=".MainActivity"
android:noHistory="true">
</activity>
Shortcut:
<shortcut
android:shortcutId="shortCutEnvironment"
android:enabled="true"
android:icon="#drawable/ic_action_temperature"
android:shortcutShortLabel="#string/shortcut_environment"
android:shortcutLongLabel="#string/shortcut_environment">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.app.myapp"
android:targetClass="com.app.myapp.EnvironmnetScreen"
/>
<category android:name="android.shortcut.conversation"/>
</shortcut>

How to show Application Launcher Dialog when do tap on button

I am getting app launcher dialog when i do tap on Home button with two options to set as home app - first, default phone app and second mine app, using this:
Activity:
public class DefaultLaunchActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
}
}
manifest.xml
<activity
android:name="com.def.launc.DefaultLaunchActivity"
android:theme="#android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
android:launchMode="singleTask"
android:stateNotNeeded="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
But what, if i have to show Application Launcher Dialog whenever user do tap on button
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// what to put here to show app launcher dialog
}
});
You need to customize some thing like below :
First : on click of button define this :
Intent intent = new Intent("com.mtetno.MYACTION");
startActivity(intent);
Second define this in manifest :
<activity
android:name=".AndroidHomeActivity" >
<intent-filter>
<action android:name="com.mtetno.MYACTION" /
</intent-filter>
</activity>
<activity
android:name=".MyActivity" >
<intent-filter>
<action android:name="com.mtetno.MYACTION" />
</intent-filter>
</activity>
Write AndroidHomeActivity activity as :
public class AndroidHomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
}

Activity B starts before Activity A

I was trying to get a small image to show up before my Main Activity starts. This is my current coding in the android manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
whatever I do, my splash screen does not start. They start separately, but never together, (I still haven't put a timer in my splash image as I want to check whether it works or not and it isn't working)
Remove
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Use below code in splash screen after desired time
Intent intent=new Intent(this,MAINACTIVITY.class);
startActivity(intent);
finish(); //To close splashscreen when MAINACTIVITY loads
The above code starts with splash screen and after some time start your main activity
You should remove the <intent-filter> section from your MainActivity declaration ,
and launch the MainActivity from the splashActivity using a simple intent and startActivity call.
Try this.
<activity
android:name=".Splash"
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=".MainActivity" >
</activity>
</application>
Code for your Splash Class-
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
public class Splash extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{ // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
};
handle.postDelayed(delay,5000);
}
}
its delay next intent 5 second. you can set time according to you.
Use This:
<activity
android:name=".Splash"
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=".MainActivity" >
</activity>
</application>
and This :
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent mInHome = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mInHome);
Splash.this.finish();
}
}, 3000);
}
}

2nd Activity won't open when Launching Activity is in Background

I have two relevant activities: Main and ShowResult. The 2nd one is launched from a Thread in Main. This works well, but as soon as Main is in background the activity won't open. Logcat doesn't show any anomalies.
Manifest:
<activity
android:name="com.....ShowResultActivity"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name="com......MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Holo.Wallpaper.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Handler inside the Thread-class / executed from Thread:
iOpen = new Intent(context, ShowResultActivity.class); // is written in the constructor
Bundle b = new Bundle(); // global
b.putInt("type", 1);
b.putString("url", value);
iOpen.putExtras(b);
context.startActivity(iOpen);
ShowResult:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
#Override
public void onNewIntent(Intent i) {
Bundle b = i.getExtras();
int value = b.getInt("type");
String url = b.getString("url");
Log.e("opened :)", value+" "+url);
if(value == 0) {
showPictureAsync(url);
} else if (value == 1) {
showVideoAsync(url);
}
}
Add this to your code
iOpen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(iOpen);

Android - MainActivity keeps on instantiating

Im very new to android development so please bear my ignorance.
I created a Splash screen before loading my main activity. Splash is working fine but what causes the problem is the main activity, it keeps on instantiating.
Splash.java
public class Splash extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
final AdController optinController = new AdController(
getApplicationContext(), "SECTION_ID");
final Splash splash = this;
optinController.loadOptin((Activity) splash, "SECTION_ID",
new AdOptinListener() {
public void onAdOptin() {
// once optin process is complete, continue to main app activity
launchMain();
}
}
);
}
public void launchMain() {
finish();
Intent myIntent = new Intent(Splash.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
}
}
MainActivity.java
public class MainActivity extends Activity {
private AdController myController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myController = new AdController(getApplicationContext(), "SECTION_ID");
myController.loadNotification();
}
}
In the manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I used android:launchMode="singleInstance" but still it keeps on reinstantiating.
Please help. Thanks in advance.
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//|Window.FEATURE_INDETERMINATE_PROGRESS
setContentView(R.layout.splash);
final Timer time = new Timer();
time.schedule(new TimerTask() {
#Override
public void run() {
Intent intent = new Intent(Splash.this,MainActivity.class);
startActivity(intent);
finish();
}
},1000);
}}
It will work , i din get you exactly by instatiating.Also do no use single instance,you are not supposed to require it here.
Yes, following is my snippet of manifest file , just use a timer for your splash screen and move to your main class after specific time.
activity android:name=".Splash"
android:screenOrientation="portrait" android:label="#string/app_name">
i think you have to remove the intent flag Intent.FLAG_ACTIVITY_SINGLE_TOP
In manifest.xml, set category as LAUNCHER which activity you want to launch first and to Other set as DEFAULT as given in below example.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Categories

Resources