0

I am building an application that needs to start when my android device first boots up which works fine. I am parsing an XML file and based off the value I get from the XML file I start another Activity from my main activity but when I started trying to start an Activity from my MainActivity onCreate I started getting the following error.

01-03 12:06:41.170    7854-7854/com.example.asiahyd.gpsdemo E/ActivityThread﹕ Performing stop of activity that is not resumed: {com.example.asiahyd.gpsdemo/com.example.asiahyd.gpsdemo.MyActivity}
java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.example.asiahyd.gpsdemo/com.example.asiahyd.gpsdemo.MyActivity}
        at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3344)
        at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3425)
        at android.app.ActivityThread.access$1100(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

This is my MainActivity onCreate method

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    check = getIntent().getExtras().getBoolean("startUp");
    setContentView(R.layout.activity_my);

    String baseDir = Environment.getExternalStorageDirectory().toString() + "/Automation";

    File f = new File(baseDir);
    int numOfFiles = f.listFiles().length;

    File[] temp = f.listFiles();

    try {
        parseData(temp[0]);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

    if(check == true){
        Intent i = new Intent(getApplicationContext(), Result.class);
        i.putExtra("coldSelected", coldSelected);
        i.putExtra("warmSelected", warmSelected);
        i.putExtra("hotSelected", hotSelected);
        i.putExtra("onDeviceLogging", 0);
        startActivity(i);
    }
}

Then this is the activity I use to start my application on device bootup

public class Bootup extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            Toast.makeText(context, "Reboot completed! Starting your app!!!.", Toast.LENGTH_LONG).show();

            Intent i = new Intent(context, MyActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("startUp", true);
            context.startActivity(i);
        }

    }
}

If anyone has any insight into why I might be getting this error I would really appreciate the help

user3404290
  • 45
  • 1
  • 2
  • 10
  • possible duplicate of [Android - Performing stop of activity that is not resumed](http://stackoverflow.com/questions/26375920/android-performing-stop-of-activity-that-is-not-resumed) – WOUNDEDStevenJones Aug 17 '15 at 18:41

1 Answers1

2

Similar problem found here: Similar Issue on SO. The solution seemed to be adding your Intent to either onResume() or onPostResume(). This is due to the app running on higher end phones.

public void onResume(){
    super.onResume();
    //XML parse and intent here
}

or:

public void onPostResume(){
    super.onPostResume();
    //XML parse and intent here
}

Also this was another viable answer:

Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
    switch (msg.what) {
        case 1:
          //start activity with intent here

        default:
            break;
    }
    return false;
}
});

And in onResume call this.

handler.sendEmptyMessageDelayed(1, 1000);

Side note: I'd also like to suggest creating an intent as follows:

Intent i = new Intent(MainActivity.this, Result.class);

I can provide a long winded answer as to why, but in short don't use getApplicationContext() unless you know why :)

Community
  • 1
  • 1
Lucas Crawford
  • 3,078
  • 2
  • 14
  • 25
  • hmm I still seem to get the error even when added to onResume and onPostResume – user3404290 Aug 17 '15 at 18:56
  • 1
    Did you try with the Handler? This delays the call a second, then tells the handler thread to start your activity. Kind of a poor solution, but does tell you that the problem is the call needs to be delayed. Also, what device are you running this on – Lucas Crawford Aug 17 '15 at 18:57
  • no worries, accept the answer so that others with your problem will know that it works :) – Lucas Crawford Aug 17 '15 at 20:51