Latest Beta version of Android SDK showcasing Default Splash Screen on every app running on Android 12 (Emulator), As per the requirements, we already have our own Splash Screen.
If anyone have worked on it, let me know how to disable/remove it (preferred to have and example code).
There is no direct API to disable the default splash screen but we can handle this with some workarounds.
Approach 1:
Add <item name="android:windowIsTranslucent">true</item> to your style
<style name="Theme.RemoveSplashScreenTheme" parent="#style/BaseTheme">
<item name="android:windowIsTranslucent">true</item>
</style>
Apply this to splash screen Activity.
<activity
android:name="com.test.SplashScreenActivity"
android:launchMode="singleInstance"
android:theme="#style/Theme.RemoveSplashScreenTheme"
android:noHistory="true" />
this will replace the default splash screen with a transparent screen. This workaround will eliminate the 2 splash screen issue if the app already has one.
But it will make the system splash screen invisible and it may look like the app is not responding. If anyone facing this issue then follow the next workaround.
Approach 2:
So we can resolve this issue by suspending the app to draw an existing splash screen and show the system splash screen until the app is ready.
private void setupOnPreDrawListenerToRootView() {
View mViewContent = findViewById(android.R.id.content);
mViewContent.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
Log.v("onPreDraw","onPreDraw called");
if (isAppInitialized) {
mViewContent.getViewTreeObserver().removeOnPreDrawListener(this);
startActivity(new Intent(this, MainActivity.class));
return true;
} else {
// The content is not ready; suspend.
return false;
}
}
});
}
Here we need to update isAppInitialized to true once the app is ready then we can remove the listener and launch the MainActivity until then it will hold the. app to draw an existing splash screen and execute all the app initializations.
I believe it’s impossible, though I very hope I’m wrong.
The docs does not mention anything about disabling it and it’s automatically being added to all apps.
I’m running Android 12 beta 2.1 on my Pixel 5 device and a lot of my apps have double splash screen because of it.
I recommend giving up migrate your code to the new API and make sure your code is compatible for Android 11 devices and below.
Related
Hello I am using this AppCompatDelegate to change between day/night themes
I have 2 activities A& B
this code called from activity B
it should recreating activity B & A with the chosen style
here is my code
applyNight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isNight) {
SharedPrefrencesMethods.savePreferences(this, getString(R.string.night_key), true);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
SharedPrefrencesMethods.savePreferences(this, getString(R.string.night_key), false);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
});
I tested it on android 7 & 6 it's working fine i.e when changing mode in activity B and press back activity A recreating with the new theme.
When trying it on android 9 it's changing the activity B only and not affecting it's parent activity A.
I was having that problem too, and then took the advice of Chris Banes in Google's official Android Developers blog https://medium.com/androiddevelopers/appcompat-v23-2-daynight-d10f90c83e94 to set setDefaultNightMode in the app's application class in the first place, so I created a class EcwgApplication extending Application as he shows, and added android:name=".EcwgApplication" in the application section of the manifest. I also put my method for switching themes in the application class as well, that my settings activity can call when the user changes the theme setting (in addition to updating SharedPreferences with the change before calling it), so it looks like this:
public class EcwgApplication extends Application {
public void onCreate() {
super.onCreate();
int selectedDarkLightTheme = PreferenceManager.getDefaultSharedPreferences(this).getInt(getString(R.string.preferences_dark_light_mode_selected_key), AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
AppCompatDelegate.setDefaultNightMode(selectedDarkLightTheme);
}
public static void setDarkLightTheme(int selectedDarkLightTheme) {
AppCompatDelegate.setDefaultNightMode(selectedDarkLightTheme);
}
}
This worked fine with Android OS versions 24 through 29, but with 21 (the lowest version this app is supporting) through 23 I would get a black screen on returning to the first activity, and while rotating the screen would fix that, it also made clear that UI state was not being saved. So I changed the StartActivity for the Settings screen to StartActivityForResult, and in onActivityResult, check if the version number <= 23, and if so, do this.recreate().
I need to keep doing more testing, but at least so far everything seems to be working great.
I've followed the example pattern for handling the android back button in the react-native docs and it works well. I can use the hardware back button to pop my navigation stack.
At the point that there's only 1 view in the stack though I don't pop it (just like the example), and I return false from my hardwareBackPress event listener. At this point it I see the componentWillUnmount method being called in my final view, at which point my app shuts down.
If I return true then nothing happens at all obviously.
What I want to happen is that the app merely gets "backgrounded" instead of exiting completely.
Answered my own question. The trick is to override the default back button behaviour in the MainActiviy:
public class MainActivity extends ReactActivity {
#Override
protected String getMainComponentName() {
return "foo";
}
#Override
public void invokeDefaultOnBackPressed() {
// do not call super. invokeDefaultOnBackPressed() as it will close the app. Instead lets just put it in the background.
moveTaskToBack(true);
}
}
Though I may be very late in giving the answer it may help other facing the issue.
Recently I came across the same requirement where I have to move the app to the background. I tried the solution provided by #pomo. Though it worked I faced problems. Sometimes on multiple clicking of the back button, the app misbehaves in android though it worked perfectly fine in iOS.
And then I came across the following issues in GitHub where it mentions the reason for the misbehaviour.
The following solution works perfectly fine now.
// android/app/.../MainActivity.java
#Override
public void invokeDefaultOnBackPressed() {
moveTaskToBack(true);
}
<!-- AndroidManifest.xml -->
<activity
...
android:launchMode="singleTop">
Link from where I get the solution
I hope I'm able to help guys with the same requirement.
How can we give separate layout or separate activity for Multiple window ?
eg. I have checked below things with help of android developer site
<activity android:name="com.configure.it.MyScreen">
<layout android:defaultHeight="400dp"
android:defaultWidth="200dp"
android:gravity="top|end"
android:minimalSize="300dp" />
</activity>
by declaring above things it affect how an activity behaves in multi-window mode.
But how can I show different layout if my particular screen is activated on Multiple-Window ?
From the Android Developer link .
To make changes in UI or separate layout which should be used on Multiple-window activate.
We can check if activity is in Multiple-window by following way
From activity Activity.isInMultiWindowMode() Call to find out if the activity is in multi-window mode.
eg. To check in Activity if its multiple window than header(or any view should have Red background color if its not in multiple window thn it should be Green background color)
headerView.setBackgroundColor(inMultiWindow()?Color.RED:Color.GREEN);
using inMultiWindow() replacing Fragment is also possible
To get Callback on Multiple-Window Activation.
From Activity onMultiWindowChanged method is available to handle runtime changes on this method callback.System will give callback on this method whenever the activity goes into or out of multi-window mode with boolean value.With the help of sample link & android developer link
#Override
public void onMultiWindowChanged(boolean inMultiWindow) {
super.onMultiWindowChanged(inMultiWindow);
headerView.setBackgroundColor(inMultiWindow ? Color.RED : Color.GREEN);
// here we can change entire fragment also.
//If multiple window in than seperate and for multiple window out different
}
Will keep updating if I get anything else.
isInMultiWindowMode() is added in API 24 to check device is in Multi window Mode or not, it returns a boolean value. whenever device goes to Multi window Mode it triggers onConfigurationChanged() method.
you will need to manually update your Views, Reload resources, etc... in onConfigurationChanged() based on Landscape and portrait mode.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//do something (Update your views / Reload resources)
}else{
}
}
In Manifest.xml
<activity
android:name=".yourActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
/>
For further reference check with Multi-Window Support Google Dev and MultiWindow Medium Corp
In any application the add/edit will be comparatively having lesser inputs. I have seen that the application, esp., calendar, are using clever strategy to show these as simple dialog, so that the user may not notice that there is empty space in the designed form
As shown below
My question is, how to make it happen?
What I'm doing is I extend DialogFragment:
public class AboutFragment extends DialogFragment { ... }
I also have an activity that contains that fragment. And when the dialog/activity needs to be called, this method decides how to display it:
public static void showAbout(Activity parent) {
if (isTablet) {
AboutFragment aboutFragment = AboutFragment.newInstance();
aboutFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogTheme);
DialogUtils.showDialog(parent, aboutFragment);
} else {
Intent aboutIntent = new Intent(parent, AboutActivity.class);
parent.startActivity(aboutIntent);
}
}
How to decide whether it is a tablet, is totally up to you.
This technique is explained in the documentation.
In my opinion the best approach here is to use
<!-- Theme for a window without an action bar that will be displayed either full-screen
on smaller screens (small, normal) or as a dialog on larger screens
(large, xlarge). -->
"android:Theme.Holo.Light.DialogWhenLarge.NoActionBar"
The best/easiest solution I've found is to always use an Activity, and based on screensize (and version), change your Theme parent.
in res/values/themes.xml
<style name="Detail.Theme" parent="#android:style/Theme.Holo.Light" >
...
</style>
and in res/values-large/themes.xml
<style name="Detail.Theme" parent="#android:style/Theme.Holo.Light.DialogWhenLarge" >
...
</style>
use Context.setTheme method to set them programmetically. As the doc says
this should be called before any views are instantiated in the Context
(for example before calling.
So, to switch between themes need to call setTheme before onCreate
public void onCreate(Bundle savedInstanceState) {
// check screen size
setTheme(dialogTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
As #StinePike answered, setting a dialog theme programatically doesn't do any use (to me), as it shows a wierd black screen behind the dialog, rather than a dimmed background (as shown in the question). This is obviously a bug.
Instead of trying to set it programatically, or in style.xml, and pretty much everywhere except for AndroidManifest.xml, I did the reverse, which has worked for me.
(the solution which I took from the marvelous solution of the above issue)
The simplest solution (that works) as follows:
1. Make the activity a dialog by default through AndroidManifest.xml:
e.g., in the AndroidManifest.xml:
<activity
android:name="com.example.MyActivity"
android:label="#string/title_activity_mine"
android:theme="#android:style/Theme.DeviceDefault.Dialog">
...
</activity>
2. On starting the activity, set the theme to default if device is not a tablet.
if (!(isTablet(this)) {
setTheme(defaultTheme);
}
super.onCreate(savedInstanceState);
...
Note:
solution will work with custom styles defined in style.xml.
Ref:
How to detect device is Android phone or Android tablet?
Dialog with transparent background in Android
Issue 4394 - android - setTheme() does not work as expected
PS: final app on tablet and phone is as follows:
Use a DailogFragment and then control how its shown with setShowsDialog()
I have this problem with the Android ActionBarCompat project: On emulators with Android 4.0 the click on the app icon doesn't cause any onOptionsItemSelected event, whereas it works on all other OS versions.
Any input is greatly appreciated!
Are you seeing any touch feedback from the app icon? (Does it glow when you press it?)
Since many activities do not use the action bar home button, in apps that target API 14+ running on Android 4.0 it is disabled by default. (This is so that users don't try pressing it, see it glow, and wonder why nothing happened.) Apps that want to use this should call ActionBar#setHomeButtonEnabled(true).
We should probably revise the ActionBarCompat sample to surface this more clearly. One simple way to get you up and running would be to modify ActionBarHelperICS.java and add the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity.getActionBar().setHomeButtonEnabled(true);
}
In an app where you want more control over turning this on and off you would want to make further changes.
I had this problem as well. This code did the trick for me:
public void onCreate(Bundle savedInstanceState) {
...
if (android.os.Build.VERSION.SDK_INT >= 11) {
//noinspection ConstantConditions
getActionBar().setHomeButtonEnabled(true);
} else {
getSupportActionBar().setHomeButtonEnabled(true);
}
}
Some extra info: minSdkVersion="7" targetSdkVersion="18". This is the LAUNCHER activity of my project, so it has no parent activity. Using setDisplayHomeAsUpEnabled(true) in other activities worked just fine.