c00ki3s c00ki3s - 2 months ago 12
Java Question

Android - Activity restarts in an endless loop

I'm creating a very simple app to do some calculations for a game and I've stumbled upon an endless activity reload loop. I have no idea what is causing this loop because there really isn't any data suggesting what to check (nothing red in my logcat).

My guess is that my IntroActivity is causing this chaos because the endless loop has a 3 second delay. I checked the SO and found multiple code samples to create this delay and nothing fixes this issue. It's really starting to annoy me. MainActivity class loads fine and layout looks ok but it reloads every 3 seconds.

I make a clean build every time I change something in my code just to make sure nothing "old" gets loaded in my build and it doesn't help. I also tried to completely isolate the MainActivity call in onCreate method inside IntroActivity class and surprisingly the screen goes blank (device goes in a loop - no errors).

This is my simple IntroActivity class with a 3 second delay to start my MainActivity where I want to create all the buttons, textfileds, etc.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class IntroActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(IntroActivity.this, MainActivity.class);
i.setFlags(i.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
}, 3000);
}
}


... and this is my MainActivity class:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends IntroActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}


This is my manifest if it helps in any way.

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".IntroActivity"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@android:style/Theme.Black.NoTitleBar">
</activity>
<activity android:name=".GameActivity"
android:theme="@android:style/Theme.Black.NoTitleBar">
</activity>
</application>


I'm building this app in Android Studio 2.2.0. My device is a Samsung Galaxy Tab S 8.4. Please let me know if you need anything else.

Any help/advice/suggestion would be appreciated.

Answer

The problem is that MainActivity is a subclass of IntroActivity. When MainActivity executes, its onCreate() method is calling super.onCreate(), which is IntroActivity's onCreate(), where you're starting MainActivity again, calling super.onCreate(), etc., ad infinitum.

It doesn't seem that MainActivity really needs to extend IntroActivity, so just make it a regular Activity subclass.

public class MainActivity extends Activity {
    ...