0

These are my two lines of code that don't launch my MainActivity. There is not error and the app does not crash. It's like these two lines of code are effectively commented out.

Intent intent = new Intent(MainActivity.mMainActivity,MainActivity.class);
MainActivity.mMainActivity.startActivity(intent); 

I initialize mMainActivity like this in MainActivity

public static MainActivity mMainActivity;
//...
mMainActivity = this;

I am trying to launch MainActivity from a non-activity class.

What is wrong? Thanks.

the_prole
  • 8,275
  • 16
  • 78
  • 163
  • where do you set mMainActivity? Looks really wrong - m stands for member usually - but this is clearly not a member ;-) – ligi Jun 23 '17 at 23:15
  • Thanks, yes it is a member, I added how I initialize it – the_prole Jun 23 '17 at 23:15
  • 1
    Please explain, **in detail**, what "don't launch my `MainActivity`" means. For example, are you crashing? If so, edit your question and provide the complete Java stack trace associated with the crash. Please note that putting an `Activity` in a `static` field is a memory leak, and therefore a bad idea. – CommonsWare Jun 23 '17 at 23:16
  • [You need a special flag if not in an Activity](https://stackoverflow.com/questions/18002599/starting-an-activity-from-broadcastreceiver-in-service/18002674#18002674). Also, don't create a static reference to your activity as it will create a memory leak. Instead, pass a reference to your non-activity in its constructor. – codeMagic Jun 23 '17 at 23:18
  • @CommonsWare Thank you, I added more details. No, there is no error and the app does not crash. Those two lines of code could be commented out and the effect or rather lack thereof would be the same. – the_prole Jun 23 '17 at 23:27
  • You're basically holding on to that reference much longer than needed. I think [this is the link of the docs to look at](https://android-developers.googleblog.com/2009/01/avoiding-memory-leaks.html). You might also want to see [this post about Context](https://stackoverflow.com/questions/987072/using-application-context-everywhere) and search other posts about application context – codeMagic Jun 23 '17 at 23:28
  • @CommonsWare thanks, I figured out my own problem, but I had a question for you about static references. I understand that static variables once they are initialized are persistent across the entire life cycle of the application. If my main activity is only every initialized just once, then what's the problem referencing it like this? – the_prole Jun 24 '17 at 00:27
  • 1
    @the_prole: "If my main activity is only every initialized just once" -- that's unlikely to be true. For example, on a configuration change (e.g., screen rotation), by default, your activity is destroyed and recreated. Now, if you reset that `static` field to the now-new instance, you will no longer be leaking the old instance. Generally speaking, putting things that reference activities in `static` fields is risky, because you start doing it, then forget to clear those fields, let the activity get destroyed (e.g., BACK button), then wind up with a leak. – CommonsWare Jun 24 '17 at 00:29

1 Answers1

0

I had MainActivity launch-mode set to singleInstance

android:launchMode="singleInstance"
the_prole
  • 8,275
  • 16
  • 78
  • 163