black screen after press back button - android

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();
}

Related

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;
}

Close/Kill Android application from recent application on soft key press

I have a overlay view over all apps. I want to close the overlay view from screen on soft key press (home, back and recent). I have a transparent activity with no content which is starting a service having overlay view.
public class SampleOverlayShowActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, SampleOverlayService.class));
}
#Override
protected void onPause() {
SampleOverlayService.stop();
finish();
super.onPause();
}
}
Inside manifest
<activity
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name="SampleOverlayShowActivity"
android:excludeFromRecents="true"
android:theme="#android:style/Theme.Dialog" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Everything is working fine expect recent apps. Activity still show up on recent application button press. It got removes when I again press recent application button.
This should be much simpler than trying to override soft key behaviour to do clean up. Instead make sure the activity is launched so that it has no recents trace. From the manifest that means adding noHistory and excludeFromRecents
<activity android:excludeFromRecents="true"
android:noHistory="true"
android:name=".."
...>
<intent-filter>
...
</intent-filter>
</activity>
Note that android:excludeFromRecents works for the task only, so you probably want to make sure that activity is a new task too, using the appropriate flags (android:launchMode="singleTask")

Up navigation goes to main activity

I have the following Androidmanifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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=".First"
android:label="#string/title_activity_first"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.MainActivity" />
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second"
android:parentActivityName=".First" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.First" />
</activity>
</application>
In main activity's onCreate method i have the following code to launch Second activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent1=new Intent(this,Second.class);
startActivity(intent1);
}
Other than that every other activities code is what android studio provide as default. But when i press up button from the Second activity it goes directly to main activity and not to the First activity. I know there are alternate solutions for this problem but i just want to know why the up navigation is behaving like this.I am new to android so i may not be understanding something really simple. Any help will be appreciated. Thank you.
If you are using calling SecondActivity in onCreate of FirstActivity, then it goes to SecondActivity. But think about it, when do an Activity actually starts?
It starts after you call onCreate, then onStart , then onResume and then your Actiivity finally starts running. In your case, you are not allowing to go further onCreate, and hence FirstActivity has never even started. And thats why when you go back in navigation it jumps to the previously opened Activity, i.e., MainActivity.
I hope this clears your answer! For reference see the Lificycle of an Activity in this link.
Because FirstActivity is not started or Created. You are calling Second directly from Main Activity. As there is no any First Activity available in application's stack how Second Activity goes to parent Activity First? It will goes to Main Activity as Its the only Activity remain in stack.

Android Application Starts with wrong activity

My Application has 3 activities (MainActivity,SampleActivity,TempActivity) and Application have to start from MainActivity because i register it in AndroidManifest as
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TempActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name=".SampleActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
</activity>
when i switch from MainActivity to TempActivity and come back to MainActivity and then close the Application then often it starts from TempActivity. after this wrong behavior when ever i start my application it starts from Wrong Activity(TempActivity). Please help me in this problem
when u go from TempActivity activity to MainActivity u give finish()
example:
Intent myIntent = new Intent(TempActivity.this, MainActivity .class);
startActivity(myIntent);
finish();
This is beacuse your activity is not destroyed, it is only paused (Check the Activity life cycle). You can override onPause to perform the desired behaviour.
How are you closing the app, means by home button application dont destroy activities, activity is stopped only. To exit your application you need to finish all of your activities in application.
Whenever you press home button you are assuming your application is closed.But it is not!!!
As per android activity life cycle you application will keep running in background and when you press application icon it will start from where it went to background.
If you are on TempActivity and if you press home button then it will start from TempActivity only.To make sure when you press home button your activity should start with MainActivity
do following changes in your code.
TempActivity.java
#Override
void onPause()
{
super.onPause();
finish();
}
So when next time you will open your application it will start with MainActivity.

"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