This question already has answers here:
Starting an activity from a service after HOME button pressed without the 5 seconds delay
(4 answers)
Closed 4 years ago.
Scenario - ActivityA is visible. On press of Home button of Android device, ActivityA/App goes in background and in onUserLeaveHint(), intent to open ActivityB is fired. The app minimises instantly but ActivityB opens after 5-6 seconds of delay. After some debugging, intent is fired immediately but onCreate() of ActivityB is called after 5-6 seconds.
PS - ActivityB has launch mode - single instance.
Any idea why is this happening?
The code to open the activity is as follows -
override fun onUserLeaveHint() {
super.onUserLeaveHint()
val intent = Intent(this, ActivityB::class.java)
startActivity(intent)
}
The ActivityB is defined as following in the manifest file -
<activity android:name=".activity.ActivityB"
android:allowTaskReparenting="true"
android:autoRemoveFromRecents="true"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:excludeFromRecents="true"
android:screenOrientation="portrait"
android:noHistory="true"
android:launchMode="singleInstance"
android:taskAffinity=""
android:supportsPictureInPicture="true"
android:theme="#style/PipTheme"/>
PipTheme -
<style name="PipTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:backgroundDimEnabled">false</item>
https://issuetracker.google.com/issues/36910222
Rather than a issue, it is most likely a framework feature which prevents app to force itself open on home button press.
There are few workarounds for this:
Using pendingIntent-
val intent = Intent(context, ActivityB::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
pendingIntent.send()
Using alarm manager to open activtiy
Related
i just want to send an broadcast when launch shortcut
after search i didn't find any solution and i find one idea its launch an activity with the No Display and send broadcast from it and i do that's like this
<activity android:name=".activity.shortcut"
android:exported="true"
android:excludeFromRecents="true"
android:theme="#style/AppTheme.Transparent"
></activity>
and the theme Transparent
<style name="AppTheme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
and the code for install shortcut its
Intent shortcutIntent = new Intent(getContext(), shortcut.class);
shortcutIntent.putExtra("name","name");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //no effect
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); //no effect
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, conversationList.getFname());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, mBitmap);
addIntent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getContext().sendBroadcast(addIntent);
after run the app and create the shortcut
when i click on the shortcut its open all the app and send brodcast
so i just want to open the activity without launch main activity in application
or just send broadcast when click on shortcut
Use a shortcut to a different activity like
<activity android:name="send">
Your app should have an activity called send and there in d onResume()
call code that sends the broadcast.
Or use a Dynamic intent and send some data part of the shortcut like
setShortLabel("send-broadcasts")
https://developer.android.com/guide/topics/ui/shortcuts.html#dynamic
I want to show dialog/popup when application in kill state and BroadcastReceiver receive any action.
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MyDialog.showDialog(context);
}
}
I have added permission in manifest.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
I am getting exception when activity foreground
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
My dialog is not display in activity kill state but receiver is called.
Actually, in this case, or without an activity, you can't launch any kind of dialog.
1. First method
create a dialog activity : How to make dialog activity
2. Second
create popup window: How to make a simple android popup window?
Edit
If the application is not in the foreground, please don't do this. Go for notification
I wanna create a custom PopupWindow similar to this :
http://android-er.blogspot.kr/2012/03/example-of-using-popupwindow.html
In this example, PopupWindow is created by a button click event from an activity, but i want to create PopupWindow via intent from another application.
Is it possible? Any comments will be very appreciated!
The idea is to declare a standard Activity to make it appear like a Popup window of sorts.
Use this code (standard boiler plate Intent code to trigger the Activity)
SOME_WIDGET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), THE_POPUP_ACTIVITY.class);
startActivity(myIntent);
}
});
If, for example, you name the popup Activity as Popup, then replace the THE_POPUP_ACTIVITY.class with Popup.class
Now, for this Activity, in the Manifest, declare a theme. For example:
And this is the corresponding style declaration:
<style name="DialogNoTitleBar" parent="android:style/Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Also, in the onCreate() of the Popup Activity, you might want to add this statement right after the setContentView(R.layout.THE_LAYOUT_XML); I say might because how you want it to appear may vary from how I program my popup Activity.
getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Your requirement isn't really clear. Intents are usually used when you want to switch to another activity or maybe send the intent to a service of some sorts. If you want to open a dialog for an action, you don't really need intents.
Create an activity with your custom design, while registering the activity in the manifest file just add this android:theme="#android:style/Theme.Dialog"
and call the activity with your intent. Hope you got it. :)
I get the following android exception when I try to open a dialog. when i am press my own SoftKeyboard key how can I fix this problem?
BadTokenException: Unable to add window -- token null is not for an application
com.example.android.softkeyboard.SoftKeyboard.diqalog(SoftKeyboard.java:759)
com.example.android.softkeyboard.SoftKeyboard.onKey(SoftKeyboard.java:526)
android.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1252)
First of all, you cannot present a dialog from a remote service, you can only do so from within a running Activity, that's why you're getting a BadTokenException. But there are solutions to this problem:
1) Present an Activity with Theme.Dialog theme:
<activity
android:name="com.srgtuszy.activity"
android:theme="#android:style/Theme.Dialog"
/>
And start the activity as a new task:
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This way, you'll get an activity which will look just like a dialog.
2) Present an empty and transparent Activity and show an AlertDialog from within the activity
Declare and start the activity in manifest just as before, but use a transparent theme:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
In the activity, override the onCreate() method and don's call setContentView() and present the AlertDialog:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context context = this;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle("Hello!");
dialog.show();
}
This is a more hacky approach, but in this way you can show a dialog to the user without dismissing the input method, you can use to to present edit options for instance.
If you just want to notify the user about a certain event, consider using Notifications, they won't distract the user and pollute the UI.
How can I use properly the Intent flag FLAG_ACTIVITY_NO_ANIMATION in AndroidManifest file? I supose my problem is trivial, but I can't find good example or solution to it.
<intent-filter>
<data android:name="android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION" />
</intent-filter>
However no error is reported by compliator, but data isn't correct.
I just want to disable animation in case switching between activities. I can use getWindow().setWindowAnimations(0); in onCreate or onResume rather but using flag is better way, isn't it?
I can use also in code:
Intent intent = new Intent(v.getContext(), newactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
getContext().startActivity(intent);
But I want to use this flag in Android Manifest. To disable animation also in case returning from second activity to first.
You can create a style,
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
and set it as theme for your activity in the manifest:
<activity android:name=".ui.ArticlesActivity" android:theme="#style/noAnimTheme">
</activity>
You can also define a style to specify custom entry and exit animations.
http://developer.android.com/reference/android/R.attr.html#windowEnterAnimation
If your context is an activity you can call overridePendingTransition:
Call immediately after one of the flavors of startActivity(Intent) or
finish to specify an explicit transition animation to perform next.
So, programmatically:
this.startActivity(new Intent(v.getContext(), newactivity.class));
this.overridePendingTransition(0, 0);
Try this code,
this.startActivity(new Intent(v.getContext(), newactivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
You can also just do this in all the activities that you dont want to transition from:
#Override
public void onPause() {
super.onPause();
overridePendingTransition(0, 0);
}
I like this approach because you do not have to mess with the style of your activity.
The line in the theme style works fine, yet that replaces the animation with a white screen. Especially on a slower phone - it is really annoying.
So, if you want an instant transition - you could use this in the theme style:
<item name="android:windowAnimationStyle">#null</item>
<item name="android:windowDisablePreview">true</item>
Here is a one-liner solution that works for as low as minSdkVersion 14 which you should insert in you res/styles.xml:
<item name="android:windowAnimationStyle">#null</item>
like so:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:windowAnimationStyle">#null</item>
</style>
...
</resources>
Cheers!
This is not an example use or an explanation of how to use FLAG_ACTIVITY_NO_ANIMATION, however it does answer how to disable the Activity switching animation, as asked in the question title:
Android, how to disable the 'wipe' effect when starting a new activity?
After starting intent you can use this code :
Intent intent = new Intent(Activity1.this, Activity2.class);
overridePendingTransition(0, 0);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
If used, intent will work with no animations or transitions
create your own style overriding android:Theme
<style name="noAnimationStyle" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
Then use it in manifest like this:
<activity android:name=".MainActivity"
android:theme="#style/noAnimationStyle">
</activity>