How to add 2 Splashscreen in application - java

The first splashscreen hidden after 5 secondes.
I want to add a second splashscreen like the first before enter in MainActivity.
in #drawable/background_1 <= This is the first image splashscreen I added.
in #drawable/background_2 <= I need to add this image in second splashscreen.
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_1" >
</RelativeLayout>
SplashScreen.java
package org.sbynight.app;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class SplashScreen extends Activity {
private static String TAG = SplashScreen.class.getName();
private static long SLEEP_TIME = 5; // Sleep for some time
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.splash);
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread {
#Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// Start main activity
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
SplashScreen.this.startActivity(intent);
SplashScreen.this.finish();
}
}
}
Problem Solved - UPDATE OF POST -
1.I create a "SplashScreen2.java" + "Splash2.xml"
2.I added #drawable>background_2 (the second image of splashscreen)
3.I added to In Manifest splash2.....
In my SplashScreen.java, I deleted this code:
// Start main activity
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
SplashScreen.this.startActivity(intent);
SplashScreen.this.finish();
In my SplashScreen.java, Replace by this code :
/**** Create Thread that will sleep for 5 seconds ****/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 1 seconds
sleep(1*1000);
// After 1 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),SplashScreen2.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
In my Splashscreen2.java, I added the same code like SplashScreen.java,
Surely with this code now to start the MainActivity.class
// Start main activity
Intent intent = new Intent(SplashScreen2.this, MainActivity.class);
SplashScreen2.this.startActivity(intent);
SplashScreen2.this.finish();
Problems Solved! I have now 2 SplashScreen!

This use-case goes against the Android design guidelines.
Please think about how your users would get a good user experience using your app and get access to your content fast.
Designing Help into Your App
Don't show unsolicited help, except in very limited cases
Naturally, you want everyone to quickly learn the ropes, discover the
cool features, and get the most out of your app. So you might be
tempted to present a one-time introductory slideshow, video, or splash
screen to all new users when they first open the app. Or you might be
drawn to the idea of displaying helpful text bubbles or dialogs when
users interact with certain features for the first time.
In almost all cases, we advise against approaches like these because:
They're interruptions. People will be eager to start using your app,
and anything you put in front of them will feel like an obstacle or
possibly an annoyance, despite your good intentions. And because they
didn't ask for it, they probably won't pay close attention to it.
They're usually not necessary. If you have usability concerns about an
aspect of your app, don't just throw help at the problem. Try to solve
it in the UI. Apply Android design patterns, styles, and building
blocks, and you'll go a long way in reducing the need to educate your
users.
Source: http://developer.android.com/design/patterns/help.html

I don't really understand why you would want to add a second "splash screen". If you really did want to do it, why not make the MainActivity your SplashScreenActivity and then move onto a new activity from there.

Related

Do not show Onboarding activity again on button click [duplicate]

This question already has answers here:
How to run an activity only once like Splash screen
(5 answers)
Closed 1 year ago.
I have a Android App built in Android studio, on this app, I am using a Walkthrough Activity.
How can set this activity in a way that when a button is clicked, this page won't show again.
This is the function "public void onFinishButtonPressed()" and this is the part where I added onlick listener to the button, how should it be done in a way that once this function is called, this activity will not open again.
I have tried to implement a code to show activity only on first time run, but it is still not the desired result, i really want this page to keep showing until using clicks on that button.
Thanks for your help in advance.
My code;
package com.frigate.vpn.view;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.frigate.vpn.R;
import com.shashank.sony.fancywalkthroughlib.FancyWalkthroughActivity;
import com.shashank.sony.fancywalkthroughlib.FancyWalkthroughCard;
import java.util.ArrayList;
import java.util.List;
public class Walkthrough extends FancyWalkthroughActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FancyWalkthroughCard fancywalkthroughCard1 = new FancyWalkthroughCard("Welcome to Frigate Media VPN", "Let me show you why so many people love Frigate Media VPN", R.drawable.find_restaurant1);
FancyWalkthroughCard fancywalkthroughCard2 = new FancyWalkthroughCard("We Protect your Privacy", "Internet access is mind free, we'll keep you safe",R.drawable.pickthebest);
FancyWalkthroughCard fancywalkthroughCard3 = new FancyWalkthroughCard("Fast & Limitless!", "We provide you the fastest servers without limits.",R.drawable.chooseurmeal);
FancyWalkthroughCard fancywalkthroughCard4 = new FancyWalkthroughCard("Frigate Media VPN is 100% Free", "You do not have to worry about paying for expensive VPN, we give you everything for free.",R.drawable.mealisonway);
fancywalkthroughCard1.setBackgroundColor(R.color.white);
fancywalkthroughCard1.setIconLayoutParams(300,300,0,0,0,0);
fancywalkthroughCard2.setBackgroundColor(R.color.white);
fancywalkthroughCard2.setIconLayoutParams(300,300,0,0,0,0);
fancywalkthroughCard3.setBackgroundColor(R.color.white);
fancywalkthroughCard3.setIconLayoutParams(300,300,0,0,0,0);
fancywalkthroughCard4.setBackgroundColor(R.color.white);
fancywalkthroughCard4.setIconLayoutParams(300,300,0,0,0,0);
List<FancyWalkthroughCard> pages = new ArrayList<>();
pages.add(fancywalkthroughCard1);
pages.add(fancywalkthroughCard2);
pages.add(fancywalkthroughCard3);
pages.add(fancywalkthroughCard4);
for (FancyWalkthroughCard page : pages) {
page.setTitleColor(R.color.black);
page.setDescriptionColor(R.color.black);
}
setFinishButtonTitle("Get Started");
showNavigationControls(true);
setColorBackground(R.color.white);
//setImageBackground(R.drawable.restaurant);
setInactiveIndicatorColor(R.color.grey_600);
setActiveIndicatorColor(R.color.colorGreen);
setOnboardPages(pages);
}
#Override
public void onFinishButtonPressed() {
Intent intent = new Intent(Walkthrough.this, MainActivity.class);
startActivity(intent);
finish();
}
}
Why not just use a global boolean variable named "isFinishedButtonPressed" set to false by default and when the button is pressed set it to true, and with the correct conditions it should do what you want, no ?

How to stop/cancel a runnable when pressing on android phone's back button?

In my project, I have an activity called "ExamMenuActivity" where I can choose between "Addition, Subtraction, Multiplication and Division" activities.
In Addition activity (called ExamAdditionActivity) I have a handler method to regenerate the question form after a given answer. Everything seems working fine, I can generate the question and give an answer and after giving an answer, a new question is generated in 2 seconds.
The issue I am having is, after I give a correct or a wrong answer to the question, within 2 seconds, if I quickly press on the back button of the phone and don't wait for the question to regenerate, I come back to the Exam Menu page as I wanted (back to ExamMenuActivity) but the screen changes back to ExamAdditionActivity and I see a new generated question again.
So I want to be able to come back to Exam Menu activity again when I press on the back button of the phone before the question regenerates and I don't want to face back the ExamAdditionActivity again with a new generated question (say I changed my mind after giving an answer to an addition question and I wanted to choose another activity from the menu and I didn't wait for at least 2 seconds).
I have tried overriding the activity with onBackPressed method:
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
but unfortunately that didn't work.
Here is how I regenerate the question. I basically restart the same activity in two seconds with a handler ( there was a runnable within my handler code but since it was showing anonymous, android studio offered me to change it to a lambda function) :
private void regenerateQuestion() {
new Handler().postDelayed(() -> {
Intent restartExamAdditionActivity = new Intent(ExamAdditionActivity.this, ExamAdditionActivity.class);
overridePendingTransition(0, 0);
startActivity(restartExamAdditionActivity);
finish();
overridePendingTransition(0, 0);
}, TIME_OUT);
}
Even though I am not sure but I think I am having the problem in button listeners area since I give an answer to the question and call regenetate() method there under each button.
I hope there can be an answer to my issue. Thank you so much for stopping by to check on my post!
Create your Handler as a member variable of your class, like this:
Handler myHandler = new Handler();
Create your Runnable as a member variable of your class, like this:
Runnable myRunnable = new Runnable() {
#Override
public void run() {
Intent restartExamAdditionActivity = new Intent(ExamAdditionActivity.this, ExamAdditionActivity.class);
overridePendingTransition(0, 0);
startActivity(restartExamAdditionActivity);
finish();
overridePendingTransition(0, 0);
}
};
Post the Runnable using the new variable:
myHandler.postDelayed(myRunnable, TIME_OUT);
When you want to cancel the regeneration, do this:
myHandler.removeCallbacks(myRunnable);

Activity doesn't close when back button is pressed

I have an application that uses crosswalk. I use it in two activities.
In this activity, I have a crosswalk view that shows a list of selectable items and when selected launch another activity.
In this new activity I open another crosswalk view that runs the selected item from the previous activity.
The issue that I am having is when in the second activity when I press the back button it goes back to a black screen. If I press the back button again, it then closes the activity.
What can I do to close the activity instead of going back to the black screen? This doesn't happen on all items just a few, and with those few I think that a page redirect is happening in crosswalk so when I press back it just goes to the previous screen.
Here is the activity:
package com.gamesmart.gamesmart;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.xwalk.core.XWalkPreferences;
import org.xwalk.core.XWalkView;
public class Play extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
XWalkView xWalkWebView = (XWalkView)findViewById(R.id.xwalkWebViewPlay);
// Turn on debugging if we are in DEBUG mode
if (BuildConfig.DEBUG) {
XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
}
// Load the url
xWalkWebView.load(url, null);
}
#Override
public void onBackPressed(){
finish();
}
}
I don't think that my onBackPressed is doing what it should be...
You forgot
super.onBackPressed();
Just use below code
#Override
public void onBackPressed() {
super.onBackPressed();
}
Try this
public void onBackPressed(){
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}
Maybe you should try to call the super method in your onBackPressed(), or if you want to go back to the previous activity, to bring to front the other activity in your onBackPressed(), and then, finish your Activity like this :
public void onBackPressed() {
Intent main = new Intent(this, YourClass.class);
main.putFlags(Intent.ACTIVITY_BROUGHT_TO_FRONT); // Or something like this, I dont really remember the flag name
startActivity(main);
super.onBackPressed();
}
Just write this command when you want to exit :
overridePendingTransition ( 0 , 0 );
super.finish ( );
super.onStop ( );
super.onDestroy ( );
overridePendingTransition ( 0 , 0 );

Android setContentView on pretty short Activity life time

The first activity of my android app, the "launcher activity", finishes pretty quickly. It's goal is to redirect to the MainActivity or to the UserLoginActivity, depending on the value of a shared-preferences variable.
If this variable does not exist, it automatically perform a StartActivity to the MainActivity.
If this variable is set, then it will perform an HTTP request to my API, in order to Authenticate the user. Then it will start the MainActivity. The HTTP request usually takes less than one second.
The thing is that I would like to display a progress bar, in the center of the LauncherActivity, so the user can understand that something is loading.
The problem is that nothing is displayed on the screen. But if I comment the line that starts the activity, then it will be displayed... It seems that the activity duration is too fast to display anything !
I thought calling the setContentView() method will instantly load Views on the screen. Is my case a normal behavior ? How could I display a progress bar on the screen, knowing that the activity will last around one second ?
Here you can see my Launcher Activity
public class Launcher extends Activity {
private void goToUserLogin(){
Intent intent;
intent = new Intent(this, UserLoginActivity.class);
startActivity(intent);
finish();
}
private void goToMain(){
YokiAPI API = new YokiAPI(this);
Intent intent;
try {
if (API.authenticateSmart(YokiGlobals.preferences.getSavedUserId(this))) {
intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} else {
this.goToUserLogin();
}
} catch (Exception e){}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
// Launch Demo if First Run
if (YokiGlobals.preferences.getFirstLaunch(this)){
YokiGlobals.preferences.updateFirstLaunch(false, this);
this.launchDemo();
}
/*
** If userId saved, smart Auth and go to Main
** Else go to User Login for Full Auth or register
*/
if (YokiGlobals.preferences.getSavedUserId(this) == null){
this.goToUserLogin();
}
else {
this.goToMain();
}
}
}
And the .xml ressource file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="THIS TEXT WONT APPEAR"
android:layout_marginTop="208dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Thanks,
Oscar
I thought calling the setContentView() method will instantly load Views on the screen
No it won't because you are still in onCreate(). If you want to see any UI you need to let the activity cycle to go further, so rework your code or move your authentication to separate activity, meybe?
PS: you use this. without any real reason.
Thank you for your help. I used AsyncTask in order to fetch data from the API.
The main UI thread can now load the view.
Here is some very simplified code that maybe could help others.
public class UserSmartAuthActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_smart_auth);
new SmartAuth().execute();
}
private class SmartAuth extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
Context context = getApplicationContext();
YokiAPI API = new YokiAPI(context);
Intent intent = null;
try {
if (API.authenticateSmart(YokiGlobals.preferences.getSavedUserId(context)))
intent = new Intent(context, MainActivity.class);
else
intent = new Intent(context, UserLoginActivity.class);
} catch (Exception e){}
startActivity(intent);
finish();
return null;
}
}
}

Android - After closing an activity, when I run the app again, two activities run at the same time. How can I avoid it?

So I've got an activity in my android app, that runs on start.
This activity is just a page with a start button.
When I press the start button, it calls another activity and closes itself:
Intent i = new Intent(this, Dictating.class);
startActivity(i);
finish();
The other Activity is using Text-to-speech to dictate some words.
Now I've got something weird happening:
1) I listen to the dictating.
2) I press back button: dictating stops (what I want)
3) I run again the app, press the start button. Now I have my new activity running and dictating, but in the back I can hear the older Activity that resumed where it was, and continues dictating.
I would like for the new activity to start all over again, and not keep the other activity.
How can I do that ?
PS: This is an activity problem, and not a text-to-speech problem as I'm flushing the text-to-speech each time, It could not be kept in the memory
Thank you
EDIT:
Here is the onCreate of my Dictating class, there is tons of code in this class, I obviously don't want to post all my code, so here is some parts:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.streaming);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
this.txtCurrentWord = (TextView) findViewById(R.id.txtCurrentWord);
this.btnPlayPause = findViewById(R.id.btnPlayPause);
this.btnPlayPause.setOnClickListener(this);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
this.tts = new TextToSpeech(this, this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
there are a few weird things I'm doing like:
Runnable task = new Runnable() {
public void run() {
//runs on ui
runOnUiThread(new Runnable() {
public void run() {
readNextWord();
}
});
}
};
worker.schedule(task, 1, TimeUnit.SECONDS);
this delays the next word by one second, and then executes a fonction in the main ui thread. not sure if this matter
And some flushing at the end:
#Override
public void onDestroy() {
tts.shutdown();
super.onDestroy();
}
You need to add launchMode property to your activity inside AndroidManifest file, for more detail see "Using the manifest file"
This question is over a year old, but I can't believe no one ever gave you the right answer. Also, I don't think you should have accepted an answer that clearly didn't solve anything for you. By accepting such answers, you're just cluttering the StackOverflow google search results with junk for other people with the same problem.
The flushing you do at the end is completely wrong. According to the Activity lifecycle, onDestroy() is never guaranteed to be called. If you want to make sure the flushing gets done properly, do it inside of onPause().
For now the solution I'm giving you does fix the main problem you've described. However, if you do get the time to do a more complete rewrite, you'll want use a service that you bind to your activity. That will give you the finer control you require.

Categories

Resources