How do you add an activity (SplashActivity.java in my case) that launches automatically before the MainActivity? I've added in the below code but the activity is still being skipped over. I've already tried going to Edit Configurations -> Launch -> Default Activity. And the Splash Activity is the only activity with an intent filter.
SplashActivity.java
package com.example.simplesplashscreen;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.widget.AppCompatImageView;
public class SplashActivity extends Activity implements InterfaceSplash {
private AppCompatImageView image;
private AnimatedVectorDrawable animation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
image = (AppCompatImageView) findViewById(R.id.splashobject);
launchSplashScreen();
dismissSplashScreen();
}
#Override
public void launchSplashScreen() {
super.onStart();
Drawable d = image.getDrawable();
if (d instanceof AnimatedVectorDrawable) {
Log.d("testanim", "onCreate: instancefound" + d.toString());
animation = (AnimatedVectorDrawable) d;
animation.start();
}
}
#Override
public void dismissSplashScreen() {
// Start Main Activity
startActivity(new Intent(SplashActivity.this,MainActivity.class));
finish();
}
}
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplesplashscreen">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.SimpleSplashScreen">
<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
<activity
android:name=".SecondActivity"
android:exported="true">
</activity>
</application>
</manifest>
Related
When I try to run the code I keep getting this error "The activity must be exported or contain an intent-filter".
I have set the small drop down beside the green run button to "app" and I have also put an intent-filter in my activity in the manifest. But this doesn't help. Hopefully someone can help me.
The code below is of my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brian.project">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginSignupActivity.LoginActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
</activity>
<activity android:name=".HomeActivity"></activity>
<activity android:name=".LoginSignupActivity.SignupActivity"></activity>
<!--<activity android:name="WeightActivity"></activity>-->
</application>
</manifest>
My MainActivity code:
package com.example.brian.project;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.brian.project.LoginSignupActivity.LoginActivity;
import com.example.brian.project.LoginSignupActivity.SignupActivity;
public class MainActivity extends AppCompatActivity {
Button Login, Create_Account;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Login=(Button) findViewById(R.id.btn_login);
Button Create_Account=(Button) findViewById(R.id.btn_create_account);
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
openLoginActivity();
}
});
Create_Account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
openSignupActivity();
}
});
}
public void openLoginActivity() {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
public void openSignupActivity() {
Intent intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(intent);
}
}
Try to use android:exported="true" in the <activity> tag:
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I was having a play with Android Studio to create a birthday card and have come to a slight problem with the audio file playing before MainActivity is even on screen. The audio file plays while the splash screen is still on the screen.
I have two Java files:
MainActivity.class
package com.example.android.happybirthday;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
MediaPlayer mySoundfile;
#Override
protected void onPause() {
super.onPause();
mySoundfile.release();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySoundfile = MediaPlayer.create(this, R.raw.music);
mySoundfile.setLooping(true);
mySoundfile.setVolume(100, 100);
mySoundfile.seekTo(0);
mySoundfile.start();
}
}
and Splash.class
package com.example.android.happybirthday;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread mythread = new Thread() {
#Override
public void run() {
try {
sleep(3000); // 3 second delay for cold start
Intent startMainApp = new Intent(getApplicationContext(), MainActivity.class); // initiate MainActivity
startActivity(startMainApp); // open MainActivity
finish(); // close this activity
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
mythread.start();
}
}
This is the AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.happybirthday">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Splash"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Could you kindly give some support?
Thanks :)
Thanks #Sergey and #You Kim, I managed to get it done via adding a delay in onStart within the MainActivity.class
you need to run the media player when the activity is started, not on the create.
try running media player on onStart() or onResume().
Called when activity resume is complete
protected void onPostResume()
i'm a beginner in Android
and i'm trying different programs but this error still show up
':app:processDebugManifest' Manifest merger failed with multiple errors
how i can fix it
this is my code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.nhn.test">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:icon,android:theme"
>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
<activity android:name=".Main2Activity" >
</activity>
</application>
<android:replace>"android:icon"</android:replace>
</manifest>
this is the first layout
package com.example.nhn.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public void BtnNex(View view){
Intent int1= new Intent(this,Main2Activity.class);
startActivity(int1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
this is the second had one button
package com.example.nhn.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
public void BtnPrev(View view){
Intent int2= new Intent(this,MainActivity.class);
startActivity(int2);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
I have two different activities in my android applications. The first(MainActivity) has ImageButton through which onClick it's getting navigate two second activity(Numbers). This is my code of MainActivity
package com.android.learning_numbers;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.ImageButton;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.button1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, Numbers.class);
startActivity(intent);
}
});
}
}
Now here there is no error in error log but when application is getting launched on click of imagebutton it is getting crashed. & showing error in LogCat. What i do now. Please help..
It would seem that you haven't declared the Numbers Activity in the Android Manifest.
If an Activity is not declared in the Manifest, as far as the application is concerned, the Activity does not exist which causes the application to crash.
<application>
<activity
android:name="com.android.learning_numbers.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.learning_numbers.Numbers"/>
</application>
Try this.. in your manifest before </application>
<activity
android:name="com.android.learning_numbers.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Numbers" />
<activity
android:name="com.android.learning_numbers.Numbers"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustResize" >
I want to create a splash screen that will then move to the login/register screen. My code looks like this:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AssaultTDActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.TimeOut();
}
public void TimeOut(){
long start = System.currentTimeMillis();
boolean continueloop = true;
long timenow;
while (continueloop = true){
timenow = System.currentTimeMillis();
if (timenow - start > 5000){
continueloop = false;
this.GoToRegister();
}
}
}
public void GoToRegister(){
Intent i = new Intent(AssaultTDActivity.this, register_activity.class);
startActivity(i);
finish();
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class register_activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
}
and my manifest file is the following:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity android:screenOrientation="landscape"
android:label="#string/app_name"
android:name=".AssaultTDActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity class =".register_activity"
android:label="Log in"
android:screenOrientation="landscape"
android:name=".register_activity" >
</activity>
</application>
So am I doing something wrong here?
Also is there a command to "do events" while looping so you dot get stuck in a loop?
Hopefully this is the issue: looks like you may have had a find/replace mistake, this line in your manifest is wrong:
<uses-Activityk android:minActivitykVersion="8" />
Change it to:
<uses-sdk android:minSdkVersion="8" />
Because you added so many activities, it will most likely be fixed if you add:
<category android:name="android.intent.category.DEFAULT" />
So your main activity is the default activity and then the Android Launcher wont get tripped up.
<activity android:name=".MainActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Taken from: http://developer.android.com/reference/android/content/Intent.html
Activities will very often need to support the CATEGORY_DEFAULT so
that they can be found by Context.startActivity()