StartActivity in application subclass

I have a small Android application in which I point my application directly and make some application-level settings in onSreate ApplicationSubclass, but I get the following error (note, I know about FLAG_ACTIVITY_NEW_TASK):

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at android.app.ContextImpl.startActivity(ContextImpl.java:644) at android.content.ContextWrapper.startActivity(ContextWrapper.java:258) at somePart.ofA.nameSpace.ApplicationSubclass.sendNotificationEmail(ApplicationSubclass.java:186) 

I call this sendNotificationEmail when several exceptions are thrown, and I catch them, so that the application user can send a small email with information about the exception so that I can more easily fix anything that might occur, or direct them to fix their problem.

Here are some of the relevant code:

manifest.xml:

 <application android:label="@string/app_name" android:icon="@drawable/icon" android:name=".ApplicationSubclass"> // ... some stuff, including all of my app activities </application> 

ApplicationSubclass is defined as:

 public class ApplicationSubclass extends Application { // Multiple methods, including an override of onCreate public void sendNotificationEmail(String emailBody) { Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); emailIntent.setType("text/html"); emailIntent.putExtra(Intent.EXTRA_EMAIL, notificationRecipients); emailIntent.putExtra(Intent.EXTRA_SUBJECT, "MyAppName Error"); emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody); try { startActivity(Intent.createChooser(emailIntent, "An error has occurred! Send an error report?")); } catch (ActivityNotFoundException e) { // If there is nothing that can send a text/html MIME type e.printStackTrace(); } } } 

I would like to know why this is happening. I read some documentation, I searched for some answers on StackOverflow and the Internet in general. It seems to me that I should be fine, as I set FLAG_ACTIVITY_NEW_TASK, however this is clearly not the case!

Is it just that I can't even do this from the context of the application? Should I be within the scope of the Activity in my application when I try to do this? It would be very unpleasant that it would be necessary to rearrange around this, and the ability to send exceptions emails would be very useful!

EDIT : Rich asked a good question, and why the hell do I want to do this first? The answer is that there is data in the application that may be crashing, and I would like the user to be able to send some of this crash data when this happens.

The reason for this in the application, and not in the action, is that there are application state variables that I don’t want to lose every time someone turns the device on. When I found out that onPause and onCreate are called on rotation (for Activies), I reorganized this code into an application (which works great!). I actually tried the solution of sending them to onConfigurationChanged and overriding this method in my activity, but this is messy and for whatever reason, despite the fact that I put android:configChanges="keyboardHidden|orientation" in my manifest, it didn’t work for me correctly. I am sure that I can pursue this option, but I would prefer to leave things as they are (it is cleaner) and get an opportunity for someone via email!

+6
source share
2 answers

Oh! I realized what I did, it's pretty simple! I set FLAG_ACTIVITY_NEW_TASK on the wrong intention! Stupid me, I missed that Intent.createChooser (..., ...) will return a new intent, so you should set the flag in the Selection Preference, and not in the ACTION_SEND intent.

Not all of this is confusing when you think about it, and I cannot believe that I did not notice it!

So, if anyone does what I did, here you are:

 public void sendNotificationEmail(String emailBody) { Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setType("text/html"); emailIntent.putExtra(Intent.EXTRA_EMAIL, notificationRecipients); emailIntent.putExtra(Intent.EXTRA_SUBJECT, "MyAppName Error"); emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody); Intent emailChooser = Intent.createChooser(emailIntent, "An error has occurred! Send an error report?"); emailChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { startActivity(emailChooser); } catch (ActivityNotFoundException e) { // If there is nothing that can send a text/html MIME type e.printStackTrace(); } } 
+17
source

I have a launch (to reinstall the user when the session is lost) from my class, which is a subclass of the application as follows:

 public boolean relogin(Activity act) { Intent intent = new Intent(act,ActivityLogin.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); act.finish();// here i finish my current activity 

}

+5
source

Source: https://habr.com/ru/post/899165/


All Articles