How to show splash screen when app is in background in Android? - android

I have a Splash screen(Couldn't avoid it, as it is for branding reasons) for my application.
I want to mask the user interface and show the splash screen when it is in background (like banking applications do).
Should I overide onPause() and onResume() for the view in MainActivity?
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity">
<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:theme="#style/AppTheme.NoActionBar"></activity>
</application>
Splash Activity :
onCreate()
setContentView(R.layout.splash_layout);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Main-Activity. */
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
MainActivity(): Just show a layout with text and buttons
onCreate():
setContentView(R.layout.example_view);
Now when my application is in background and When I press Menu button to see list of applications in my stack, I should see the splashview(the xml file) not the deafult behaviour i.e MainActivity layout.
One of the things I tried out is adding
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
which does mask the application when it is in background with White Screen but I'm trying to show the splash view when the app is in background.
I know all the iOS Banking application has this feature.
Edited :
If you see the twitter app(Example) in the background, I want to mask the application view with splash screen. Right now using FLAG_SECURE helps me to mask the screen with white screen but I want to show Splash layout rather than white screen.
If you see the below picture from an iOS device - DISCOVER application (example) masks the app with splash screen, Can this be done on Android?

Can this be done on Android?
Not directly, AFAIK.
Should I overide onPause() and onResume() for the view in MainActivity?
Probably not. While you are welcome to change your UI there:
Those methods get called for other reasons (e.g., when the user taps on another window in multi-window mode, when you launch another of your activities)
That may be too late with respect to when the screenshot is taken

Related

black screen after press back button

My app contains only one activity and ads activity. It works well, but when I press "Back" button my application is closed and I can see, instead of wallpaper, black background which is partly rolled down. I can see all icons as well. Then black part of screen disappears and I can see my normal screen wallpaper. It takes only less than a second but it is annoying. I can't found what the problem is it.
My activity has only horizontal orientation and has correct life cycle (onPause-onStop-onDestroy) after pressing back button. Ads activity is not launched at all.
I did not override onPause() onStop() onDestroy().
My Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/icon_transp_bkg"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="MainActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|screenSize"
android:theme="#style/PlayTheme2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
Could it be related to back stack and trying to search some previous activity?
Please, put me on the right way to solve the problem. Thanks a lot in advance.
You can use OnBackPressed
#Override
public void onBackPressed() {
// what you want to do when back button is pressed
}
You can override the OnBackPressed method in the activity
#Override
public void onBackPressed(){
finish();
}
And call on the FINISH method, this will properly close both your activity and app.
Keep the Back-Stack and Trying to search some previous activity.
Ovverride the onBackPressed with a Call to Finish to End Both.
public void onBackPressed(){
finish();
}

Android App opens second instance when "launched" after back button pressed

Short Version:
An application I have been developing opens a second instance when it is "launched" after the back button has been pressed.
Long Version:
When the application is launched from the "home screen" it works fine- if the application is sent to the background by pressing the back arrow button, by pressing the circle button, or the square button to change to a different app - and then the application is resumed through the multi-tasking menu it works perfectly - resuming exactly where it left off - not showing the splash screen again and most importantly not opening a second instance. However if the application is sent to the background and then launched again from the home screen a second instance of the application is created! The splash screen displays again and the main activity is displayed on the screen rather than the last activity that was on the users screen! This is obviously not the desired behavior and many solutions I have attempted do not work.
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:launchMode="singleInstance">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
</activity>
<activity
android:name=".Settings_area"
android:screenOrientation="portrait" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyDieXTCaFoIL0kJ_IM4UMBSQL3sNn92AWM" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" />
<activity android:name=".Splash"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".aboutPageActivity" />
<activity android:name=".turnOffFromNotification"
android:noHistory="true"></activity>
</application>
Edit:
I have also attempted to replace launchMode in the application tag with singleTask and singleInstance.
However if the application is sent to the background and then launched again from the home screen a second instance of the application is created! The splash screen displays again and the main activity is displayed on the screen rather than the last activity that was on the users screen!
This is an expected behavior of Android. The back button (its default implementation) is intended for a user to notify the system that he/she is done with the current Activity letting Android to destroy the Activity's instance.
Note, such a behavior has nothing in common with the android:launchMode attribute.
This is obviously not the desired behavior and many solutions I have attempted do not work.
You can't change it. It's a matter of a user who decided to leave the Activity and OS destroying the Activity's instance no more needed.
The situation was resolved by removing android:launchMode="singleInstance"> from everywhere except the <application> tag. Apparently launch mode is best used when the goal is to create another task - which was not the desired behavior. Stock Android masked this behavior while the modified version of Android with Touchwiz did not.
It saves the current Instance...(it will not destroy it).
when you open app again that time it will reopen the same Instance.
import android.view.KeyEvent;
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
moveTaskToBack(true);
return true; // return
}
return false;
}

app icon on click event from home screen

Each time my app icon from home screen is clicked, I want the app to start my FirstActivity.java. The problem now is, say I have navigated through my app and is in the ThirdActivity.java and I have clicked the home Button and go to check out my other apps. Again when I click the app icon in home screen, my Application starts from ThirdActivity.java. I want it to start from FirstActivity.java.
I have used clearTaskOnLaunch="true" in manifest for my FirstActivity.java and also checked using android:finishOnTaskLaunch ="true" in all my other activities but the problem is still there. How can I solve this?
All I have to do was declare android:launchMode="singleTask" in manifest for my FirstActivity.java "only". By doing this, every time user selects the app by clicking the app icon in the home screen, app will start with FirstActivity.java
Say the user has navigated through my app and is in the ThirdActivity.java and clicked the home Button. The user has the option of selecting the app again from the background apps section (task manager section). If he/she does so, it will start from where he/she has left (ThirdActivity.java). Only if they click the app icon will the app start from FirstActivity.java
you need to finish you current activity at the time when you are launching intent for new activity, there is a method to do that finish();
In your oncreate you just have to start your activity using Intent
In manifest you have to provide name attribute to application tag
<application
android:allowBackup="true"
android:icon="#drawable/app_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name=".class name which extends Application"
>
Here you can see details on how to use application class:
http://www.intertech.com/Blog/androids-application-class/
http://developer.android.com/reference/android/app/Application.html
http://androidexample.com/Global_Variable_Or_Application_Context_Variable_-_Android_Example/index.php?view=article_discription&aid=114&aaid=136
You can use this code in onPause() of the second third activity but you have to take care of if onPause() is called because of clicking home button or by any other mean, Code is:
Intent intent = new Intent(AnyActivity.this,FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();
You can use moveTaskToBack(true) when you click on the homebutton and start the activity you want to.
I had resolved the issue by adding all three tags android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:finishOnTaskLaunch ="true"
in manifest splash screen activity may be helped some one:
<activity
android:name=".SplashscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:finishOnTaskLaunch ="true"
android:screenOrientation="portrait"
android:theme="#style/AppThemeForAppCompact">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

I click HOME button and the program minimizes; but when I press the app icon, the program is launched twice

I have the following problem:
When I press the Android HOME key, I can see the "Desktop" and my app icon. Then I press my app icon and my application launches twice. I don't want open my app twice.
How my program works:
I have 4 Activities (A, B, C, D).
A - The Main Activity: It is the first to open. It opens the other activity that has a lot of buttons. It's like a Java's main() method. I show a SplashScreen and I call another Activity. Then I finish my activity "A".
B - The Menu Screen: In this activity, I have some buttons, like a menu. I have a configuration button, update button, and Login Button. When I click the login button, I finish this activity and open the Login Screen (Activity "C").
C - The Login Screen: The user writes the Login and Password. If the login is successful, I finish this activity and open the Activity "D".
D - The application main screen: It stays opened all the time and launches another Activities. I finish this when I want close my application.
P.S.: I tried change the launchMode flag (androidManifest.xml), but didn't work.
My AndroidManifest.xml bellow:
<application android:label="#string/app_name" android:icon="#drawable/icon" android:name="MyApplication">
<activity android:name="A"
android:label="#string/app_name"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="br.com.site.B" android:label="#string/app_name" />
<activity android:name="br.com.site.C" android:label="#string/app_name" />
<activity android:name="br.com.site.D" android:label="#string/app_name" />
</application>
And this is my Activity "A.java" source:
public class A extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
startActivity(new Intent(this, AtualizaDadosFrame.class));
}
}
I don't want open my app twice.
Thanks in advance!
I'm going to assume that you started the app initially (the first time) from an IDE (like Eclipse or IntelliJ). If so, this is a known bug in Android (see http://code.google.com/p/android/issues/detail?id=26658 ). Many people have struggled for days chasing this problem :-(
Please don't change the launchMode. This is not the correct way to solve this problem. The default (standard) launchMode is the one that works in most cases.
EDIT (Added link to workaround):
A simple workaround for this problem can be found at http://code.google.com/p/android/issues/detail?id=2373#c21
You should set the desired launch mode in your AndroidManifest.xml.
You can restrict this.....
Please go through below link.
Home key press behaviour

"invisible" toggle app without any window shown

I'm writing simple APN toggle app. I wanted to ask how to force android not show any window.
Currently after running my app, for brief time Black screen with app name is shown and then disappears.
Is it possible not to show anything ( only Toast message ) ?
public class ApnSwtichActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (toggleAPN()){
Toast.makeText(this, "Apn switched", Toast.LENGTH_SHORT).show();
}
this.finish();
}}
Sounds like you want an activity without a UI
How to launch an Activity without a UI?
You'll probably want to use
android:theme="#android:style/Theme.NoDisplay"
I don't think this is possible exactly as you want. You can hide title bar like this: add android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen" in your manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".TestActivity"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen"
>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You could create an activity, make it transparant and give it the parameters FLAG_NOT_TOUCH_MODAL & FLAG_NOT_TOUCHABLE
(FLAG_NOT_TOUCH_MODAL passes any touch input you give to your activity to the underlying screen, FLAG_NOT_TOUCHABLE negates any touch input commands for the activity)
Don't forget to make your activity autoclose after you're done

Categories

Resources