Whenever I try to exit from my app by pressing back button twice, splash screen appears and it freezes until i press back button again. So I need to press back button three times to exit from my app. Please help me to exit from app with only two back button press.
The java code in project as follows:
public void onBackPressed()
{
if (doubleBackToExitPressedOnce)
{
super.onBackPressed();
MapsActivity.this.finish();
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click Back again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
my splash screen code as follows:
public class SplashScreen extends AppCompatActivity {
ImageView logoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Animation anim1 = AnimationUtils.loadAnimation(this,R.anim.anim_down);
logoView = findViewById(R.id.logoview);
logoView.setAnimation(anim1);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
},2500);
}
}
you should to finish splashActivity after start new activity, so use :
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
SplashScreen.this.finish();
}
},2500);
Add finish(); after below code.
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
You must finish() splash screen like as below than your condition on BackPress work
Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
startActivity(next_scrn);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
Related
I want to let users choose background colors with animation in BackgroundActivity and saving that changed background color into MainActivity.
When a user clicks backgroundChange button on MainActivity, it moves to BackgroundActivity. Then there are a few different colors to choose. After a user click Save button after choosing color on BackgroundActivity, it is going back to MainAcitivity. My problem is I don't know how to save that changed background color from BackgroundAcivity to MainAcivity.
As a beginner, I cannot understand well how to use SharedPreferences.
I checked several videos and searching many questions for hours about it, but still, I cannot figure out how to use SharedPreferences correctly on my own code.
BackgroundAcivity is really long that I will only put the first part. Could you tell me how to save this background change?
MainActivity
public class MainActivity extends AppCompatActivity {
Button backgroundChange;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backgroundChange = findViewById(R.id.backgroundChange);
backgroundChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, BackgroundActivity.class);
startActivity(intent);
}
});
}
}
BackgroundActivity
public class BackgroundActivity extends AppCompatActivity {
Button btn_blue, btn_purple, btn_orange, btn_save;
View holderBg, dynamicBg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_background);
btn_save = findViewById(R.id.btn_save);
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(BackgroundActivity.this, MainActivity.class);
startActivity(intent);
}
});
btn_blue = findViewById(R.id.btn_blue);
btn_purple = findViewById(R.id.btn_purple);
btn_orange = findViewById(R.id.btn_orange);
holderBg = findViewById(R.id.holderBg);
dynamicBg = findViewById(R.id.dynamicBg);
//set the first-time background
holderBg.setBackgroundResource(R.drawable.bg_blue);
holderBg.setScaleY(3);
holderBg.setScaleX(3);
//set the scale of button clicked
btn_blue.setScaleY(1.5f);
btn_blue.setScaleX(1.5f);
btn_blue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//scale animation
btn_blue.animate().translationY(20).scaleX(1.5f).scaleY(1.5f).setDuration(800).start();
//default the scale buttons
btn_purple.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
btn_orange.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
//change the background
dynamicBg.animate().scaleX(3).scaleY(3).setDuration(800).start();
dynamicBg.setBackgroundResource(R.drawable.bg_blue);
//change color of button
btn_save.setTextColor(Color.parseColor("#3498db"));
//timer for change the holderbg
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
holderBg.setScaleX(3);
holderBg.setScaleY(3);
holderBg.setBackgroundResource(R.drawable.bg_blue);
dynamicBg.setScaleX(0);
dynamicBg.setScaleY(0);
}
}, 850);
}
});
}
}
You can use a bundle to transfer your data across Activity.
Saving data
Intent intent = new Intent(context, YourActivity.class);
intent.putExtra(KEY, "your value here");
startActivity(intent);
Then to retrieve data.
Intent intent = getIntent();
if (intent != null ) { //Null Checking
String strData= intent.getStringExtra(KEY);
// do your work with `strData`
}
Hello guys how are you today I hope you are OK
I have an application with many activity's :
Main-activity
activity1
activity2
activity3
what I need to do is make the activity3 popup every (x) minute, for example, every 2 or 3 minutes the activity3 popup how can I do that and thanks in advance.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//load the xml file for the starting loading screen
setContentView(R.layout.activity_main);
Log.d("MainActivity:", "onCreate: created activity_main.xml UI succesfully.");
new Timer().schedule(new TimerTask(){
public void run() {
startActivity(new Intent(MainActivity.this, PrimaryActivity.class));
finish();
Log.d("MainActivity:", "onCreate: waiting 5 seconds for MainActivity... loading PrimaryActivity.class");
}
}, 5000 );
}
}
I didn't know how to try this before posting so I need your help thanks in advance.
'Make the popup function static and call it with class name like'
yourclassname.popupFunction(Context);
'in on create method where you want to open the primary activity'
public static void popupFunction(Activity activity){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(activity, PrimaryActivity.class));
finish();
Log.d("MainActivity:", "onCreate: waiting 5 seconds for MainActivity...
loading PrimaryActivity.class");
}
},5000);
}
'Hope you get the answer Thanks'
I'm trying to integrate Interstitial Ad in splash screen activity
by using this tutorial
.. but the ad not loading.
can anyone tell me where is the problem please?
thanks in advance
here is my code :
public class SplashScreenActivity extends Activity {
private InterstitialAd mInterstitialAd;
private Timer waitTimer;
private boolean interstitialCanceled;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
mInterstitialAd.setAdListener(new AdListener()
{
#Override
public void onAdLoaded() {
if (!interstitialCanceled) {
waitTimer.cancel();
mInterstitialAd.show();
}
}
#Override
public void onAdFailedToLoad(int errorCode) {
startHomeMain();
}
});
waitTimer = new Timer();
waitTimer.schedule(new TimerTask() {
#Override
public void run() {
interstitialCanceled = true;
SplashScreenActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
startHomeMain();
}
});
}
}, 5000);
} // end of onCreate implementation.
private void startHomeMain() {
Intent intent = new Intent(this, HomeMain.class);
startActivity(intent);
finish();
}
#Override
public void onPause() {
waitTimer.cancel();
interstitialCanceled = true;
super.onPause();
}
#Override
public void onResume() {
super.onResume();
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else if (interstitialCanceled) {
startHomeMain();
}
}
}
I'm trying to integrate Interstitial Ad in splash screen activity
by using this tutorial
.. but the ad not loading.
can anyone tell me where is the problem please?
thanks in advance
Google ads takes minimum 6 seconds to Load , and in your code you have set timer of 5 seconds , so thats why your Splash screen activity finished but ad not shown
try to set timer of greater than 6 seconds , and display add on destroy method of activity.
Remove timer and add progress dialogue and in onAdLoaded Dismiss dialogue and show ad as well as Dismiss dialogue in onFailedtoLoad
So I've implemented a double press to exit on my mainactivity which exits fine if I go to settings activity or to game activity. However, if I go mainactivity --> gameactivity --> gameoveractivity and then press back it starts the mainactivity fine but then double pressing just returns to gameoveractivity and I'm not sure why as I call finish when starting mainactivity from gameoveractivity.
mainscreenactivity:
// button listeners
playButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent gameActivityIntent = new Intent(MainScreenActivity.this, GameActivity.class);
startActivity(gameActivityIntent);
overridePendingTransition(R.anim.righttocenter, R.anim.centertoleft);
finish();
}
});
settingsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent settingsActivityIntent = new Intent(MainScreenActivity.this, SettingsActivity.class);
MainScreenActivity.this.startActivity(settingsActivityIntent);
overridePendingTransition(R.anim.righttocenter, R.anim.centertoleft);
}
});
gameactivity:
// handle hardware back button
#Override
public void onBackPressed() {
Intent mainScreenActivityIntent = new Intent(GameActivity.this, MainScreenActivity.class);
startActivity(mainScreenActivityIntent);
overridePendingTransition(R.anim.lefttocenter, R.anim.centertoright);
finish();
}
logic code to handle going to gameoveractivity:
if (weight.getBounds().intersect(player.getBounds())) {
timer.cancel();
gameTimer.cancel();
player.setTouched(false);
save(score, time);
Intent gameOverIntent = new Intent(this.getContext(), GameOverActivity.class);
this.getContext().startActivity(gameOverIntent);
((Activity) this.getContext()).finish();
}
gameoveractivity:
// handle hardware back button
#Override
public void onBackPressed() {
Intent mainScreenActivityIntent = new Intent(GameOverActivity.this, MainScreenActivity.class);
mainScreenActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
GameOverActivity.this.startActivity(mainScreenActivityIntent);
overridePendingTransition(R.anim.lefttocenter, R.anim.centertoright);
finish();
}
Edit: Managed to fix this by adding this: gameOverIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in the game logic code after creating the gameoveractivityintent.
Managed to fix this by adding this:
gameOverIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in the game logic code after creating the gameoveractivityintent.
I have three images with me and i want them to appear on first layout xml like a splash view so that they can be viewed only once i.e that activity will be called only once when app get's installed or if app get's a new update otherwise app should always start from the Second activity, i don't know how should i begin with this :
Can any one tell me any idea how this can be done.
To show splash for only once.
Next part of this question is here
Coding will be much appreciated.
Save a flag in the Preferences when you start up the application, after you've done the welcome screen stuff. Check for this flag before you show the welcome screen. If the flag is present (in other words, if it's not the first time), don't show it.
In your activity:
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// second argument is the default to use if the preference can't be found
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
if (!welcomeScreenShown) {
// here you can launch another activity if you like
// the code below will display a popup
String whatsNewTitle = getResources().getString(R.string.whatsNewTitle);
String whatsNewText = getResources().getString(R.string.whatsNewText);
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit(); // Very important to save the preference
}
}
Try this :
public class MainActivity extends Activity {
private Thread mSplashThread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
final MainActivity sPlashScreen = this;
mSplashThread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
wait(4000);
}
} catch (InterruptedException ex) {
}
finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen, StartNewActivity.class);// <-- Activity you want to start after Splash
startActivity(intent);
}
};
mSplashThread.start();
} catch (Exception e) {
}
}
#Override
public boolean onTouchEvent(MotionEvent evt) {
try {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notifyAll();
}
}
} catch (Exception e) {
}
return true;
}
}
you put an Image in splash.xml to show
to do this you have to detect the first launch of your application. To do so you can store a boolean value as #Nirav suggested.
And for the splash screen, You can consider using Fragments and ViewPager to create an activity which will only be shown for the first time