Alarm Activity gets launched on launching the app - android-activity

Scenario : I have a simple to-do app where In the main activity I have a fragment that takes a list of to-dos from the user. I set an alarm for a fixed time in the future (in my case every 2 min ONLY for testing. The AlarmReceiver class launches an AlarmActivity which shows the list of to-dos as checkboxes for the user to mark as done.
When the alarm comes up and shows the list, I have a "Submit" button to mark all the checked items as 'done' in the local db.
Problem:
This AlarmActivity disappears on click of submit since I'm calling finish on it. But when I launch the app from the icon, the AlarmActivity is shown instead of the main activity.
AlarmReceiver code :
final Intent intent1 = new Intent(context, CheckListAlarmActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(intent1);
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:noHistory="true"
android:clearTaskOnLaunch="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AlarmActivity"
android:alwaysRetainTaskState="false"
android:clearTaskOnLaunch="true"
android:showOnLockScreen="true"
android:screenOrientation="sensorPortrait"
android:noHistory="true"/>
<receiver android:name=".AlarmReceiver" />
</application>
I added the android:clearTaskOnLaunch="true" and that seems to do the trick sometimes but not always.
And in the background when I put the loggers, it shows that every time I launch using the app icon it calls the onCreate of the MainActivity and then immediately calls the onCreate of the AlarmActivity.
I have tried looking for all kinds of solutions and tried setting a bunch of flags to the Intent. Nothing seems to do the trick convincingly.
It seems like a simple problem but I am not able to find the solution. Any help/guidance would be appreciated.

Related

I am getting so many errors in AndroidManifest.xml. How can I solve them?

Following is my code:
the errors I'm getting are:
Unresolved class 'FlutterApplication'
Attribute android:icon is not allowed here
Unresolved class 'MainActivity'
Attribute android:launchMode is not allowed here
Attribute android:theme is not allowed here
Attribute android:configChanges is not allowed here
Attribute android:hardwareAccelerated is not allowed here
Attribute android:windowSoftInputMode is not allowed here
<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="BMI Calculator"
android:icon="#mipmap/bmi_calculator">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="#style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in #style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

Android cannot set screenOrientation,

I have read through every post I could find on this and other sites and followed the advice, but in the most simple implementation I cannot get the Note 5 to set to Portrait mode when it is physically in Landscape orientation.
I added the lines as recommended to the manifest file, rebuilt and the app starts in Landscape mode.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twistlogic.miwidget">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I also tried doing this with code, which is my main purpose:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
nothing is produced that works.
I am trying to learn Android so I understand this may well be due to my limited knowledge. What I am trying to do is fix the orientation and lock it there, then allow it to change only when the user pushes a button. I did read that the screen size had to be set after API 13. But I am not certain how to do so. I am using a fragment template screen from the basic activities when the project was created.
Thanks for any help.
This can be set in the Main Activity I have not tried to set it in the Manifest here is a single line of code and a few lines to write a test Ok here is the Manifest code at the end
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int orientation = getResources().getConfiguration().orientation;
if(orientation == Configuration.ORIENTATION_PORTRAIT){
}else if(orientation == Configuration.ORIENTATION_LANDSCAPE){}
In your manifest file after your main activity Paste below line.
android:screenOrientation="portrait"
I have not tested this code in a Manifest file

How can I change starting activity in Basic4Android?

Is there a way to change starting activity in Basic4Android?
Always Main activity automatic added and set the starting activity.
Program not let to change its name too...
Is it possible?
Go to: "Project" -> "Manifest Editor" and a popup window with Android manifest xml code will show up. Insert this code after AddManifestText(... and change yourActivityName to what you want.
<activity android:name=".yourActivityName" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can run Activity.Title= "YourTitle" in any activity to display alternative name in the title bar if that is why you are looking to change the startup activity.
There is no way, I think it is not possible.
You can switch names of two activities in the manifest editor which would effectively start your app from activity2 code module:
SetActivityAttribute(Main, android:name, ".activity2")
SetActivityAttribute(Activity2, android:name, ".main")

Every activity shows its own icon

I have a few activities in my app, but for some reason on the menu there are a lot of
Icons of my app- for every activity.
How can I fix it and make an Icon for MainActivity only?
Ty.
In your manifest remove these lines from all Activities except the one you want to open on:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
(I recommend removing them from all the activities exept MainActivity).

Getting Main Activity from Main Application

I have the following manifest.
<application
android:name=".MyMainApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MyMainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I also have the following global static function.
MyMainApplication application = MyMainApplication.instance();as technique described in Using Application context everywhere?
I was wondering, out from application variable, is it possible for me to get MyMainActivity?
There is no way to retrieve the application's main Activity using the Application instance.
Usually there is no reason to do this... perhaps there is a better solution than manipulating the main Activity directly as you apparently are doing. Maybe you should update your post explaining specifically what you are attempting to do.
You can start the activity form the MyMainApplication class though it doesn't extends Activity
In Main Application Class create new Intent and set the intent flags to Intent.FLAG_ACTIVITY_NEW_TASK . Activity gets instantiated. I have tested and verified.
code snippet for reference :
Intent intent = new Intent(this,MyMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);