I am new to Java android development and i am trying to add splash screen before main activity so i created a splash screen class . and in my android manifest i did the following changes:
<activity
android:name="com.example.mayoenglishconversation.SplashScreen"
android:theme="#style/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.mayoenglishconversation.MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
and here is the code of my SplashScreen.java file:
package com.example.mayoenglishconversation;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SplashScreen extends AppCompatActivity {
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle
persistentState) {
super.onCreate(savedInstanceState, persistentState);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i=new Intent(SplashScreen.this,MainActivity.class);
startActivity(i);
}
},3000);
}
}
but issue is i am stuck with the splash screen . what i am doing wrong?
Try to change the override method protected void onCreate instead of public void onCreate. I faced this issue previously, It worked.
try to use following override method;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
Related
This is for a splash screen. I've followed the tutorial but it's still not working. It keeps on getting an error.
This is my code:
package id.ac.umn.finalproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
new Handler().postDelayed(startActivity(startApp), 3000);
}
}
The correct way to create a Splash screen is as follows
style.xml
<style name="SplashStyle" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowBackground">#drawable/splash</item>
</style>
Splash.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/black" /> // background color
<item
android:drawable="#drawable/ic_launcher" /> // logo
android:gravity="center" />
</layer-list>
Manifest
<activity
android:name=".SplashActivity"
android:exported="true"
android:noHistory="true"
android:theme="#style/SplashStyle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_splash); // No need setContentView
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
In Kotlin try this:
Handler(Looper.getMainLooper()).postDelayed({
Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
startActivity(startApp)
}, 3000)
I know this is asked many times, but I want to clear things up
In which activity should I place onDestroy()? Say I am in the MainActivity A and I move to activity B. But then I close the application while being on activity B. Does the onDestroy() method in MainActivity A get called? Or should I put an onDestroy() method for every activity in the application? Or is there any other way around this?
I have tried reading documentation and reading other answers but they don't help me clearly.
There are multiple ways to do so , Here I have mentioned the most reliable way I have found during development carrier . You need to create a Service which can keep an eye whether application got closed or not, so here I have mentioned the code !
1.Create Service , as you can see below there is a new class named as MyService, this needs to extended to Service.
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onTaskRemoved(Intent rootIntent) {
Log.d(getClass().getName(), "App just got removed from Recents!");
}
}
2.Add this entry to Manifest.xml as below ...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
>
<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.TestApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
3.This is Just a Service that will run in background , but we need to start it once the application starts , so in your launcher activity start it like below.
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent stickyService = new Intent(this, MyService.class);
startService(stickyService);
}
}
4.Service will start and whenever User will Swipe out the application from recent , the function onTaskRemoved will be called automatically , in this function you can put your required work or code to be executed at the application end !
First I create app of Navigation Drawer Activity and travel from fragment to new activity, Now I want to set Home Button on New activity which navigates to main activity of navigation drawer.
but when i run it from app it gives me this when Home button is click
but i want to go direct on MainActivity
Here
NewActivity.java
package com.shubham.navi_demo;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class NewActivity extends FragmentActivity {
private static Button Button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
onclickbutton();
}
public void onclickbutton(){
Button = (Button)findViewById(R.id.button);
Button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.MAIN");
startActivity(intent);
}
}
);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewActivity">
<intent-filter>
<action android:name="com.shubham.navi_demo.NewActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You don't have to start activity with Intent. Just call finish() in NewActivity, it will show you the Main Navigation drawer activity.
hope this helps.
Intent intent = new Intent(this,MainActivity.class);
Change
Intent intent = new Intent("android.intent.action.MAIN");
to
Intent intent = new Intent(this,MainActivity.class);
or call finish(); to finish() current Activity
Simple solution that you can provide users to navigate back to home screen is explained here.
developer.android.com/training/implementing-navigation/ancestral.html
Add this line to your 'AndroidManifest' under relevant activity declaration.
android:parentActivityName="com.example.your_main_activity"
Added an OnClickListener to my button. I would like it to switch to 'Step1' activity when I click the button. When I debugged it, I clicked on the button but nothing happened. It stayed on 'MyActivity' activity. Here's my main activity:
package apk.olley102.roothelp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyActivity extends Activity {
Button step1btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
step1btn = (Button) findViewById(R.id.step1btn);
step1btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStep = new Intent("apk.olley102.roothelp.Step1");
startActivity(openStep);
}
});
}
}
I added the button in the activity_main.xml and associated the layout to the activity. Also added MainActivity.java and Step1.java to the AndroidManifest.xml
Any help would be appreciated!
Usually to launch an activity you specify the current one instance and the one you want to open. In your case it would be something like this:
Intent openStep = new Intent(MyActivity.this, Step1.class);
startActivity(openStep);
If you still want to launch it using an action string, check that your activity declares the intent filter in AndroidManifest.xml:
<activity android:name=".Step1">
<intent-filter>
<action android:name="apk.olley102.roothelp.Step1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Also note that the action string does not need to match your activity name: you could name it apk.olley102.roothelp.GOTOSTEP1 for example and use the same string when you create the intent.
I have finally got my button working. I had to use '.DEFAULT' in the category of my second activity.
<activity
android:name=".Step1" android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="apk.olley102.instructroot.Step1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Also I had to specify the activity '.this' in my intent.
step1btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStep = new Intent(MyActivity.this, Step1.class);
startActivity(openStep);
}
});
I debugged my app and now the button launches Step1 on click. Thanks for all the help! :)
Can someone tell me whats wrong?? lost
Thanks,Mike
P.S.
I figured this might work instead of the splash for 1st page.
page1 or activity 1
package com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class test1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),test2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
page2 or activity 2
package com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class test2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.Button02);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}) ;
}
}
Manifiest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".test1"
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=".Activity2"></activity>
</application>
</manifest>
What is really wrong is you posting a question on StackOverflow, asking "Can someone tell me whats wrong??", without even taking the time to describe what symptoms you are experiencing. You are lucky that I am in a pretty good mood right now.
There are at least two problems:
Your manifest refers to an Activity2 that does not appear to exist
Your activity test2 is not defined in the manifest
I am guessing you just created your manifest incorrectly, seems that you have test2 as the class but tell the manifest it's name is Activity2.