startActivity within a subclass of Application

后端 未结 2 1391
清歌不尽
清歌不尽 2020-12-31 22:50

I have a little Android application in which I specify my application directly and do some application-wide setup in the ApplicationSubclass\' onCreate, but I am getting the

相关标签:
2条回答
  • 2020-12-31 23:14

    Ah! I figured out what I did, it's quite simple! I was setting the FLAG_ACTIVITY_NEW_TASK on the wrong intent! Silly me, I missed that Intent.createChooser(...,...) will return a new Intent, so you must set the flag on the chooser Intent rather than on the ACTION_SEND Intent.

    Not all that confusing when you think about it, and I can't believe I overlooked that!

    So if anyone ever does what I did, here you go:

    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();
            }
        }
    
    0 讨论(0)
  • 2020-12-31 23:26

    I have launch an activity(to relogin the user when loss the session) from my class which subclass from Application as follow:

    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
    }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题