I'm new to Android Studio and try to develop my first application.
I have 3 activities MainActivity, DashboardActivity, and SplashActivity.
DashboardActivity -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Hooks();
allQuestionsList=listS;
Collections.shuffle(allQuestionsList);
modelClass = listS.get(index);
setAllData();
}
SplashActivity-
public class SplashActivity extends AppCompatActivity {
public static ArrayList<ModelClass> listS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity_main);
listS = new ArrayList<>();
listS.add(new ModelClass("here is the question", "a", "b", "c", "d", "answer"));
listS.add(new ModelClass("Under-inflated tires increase fuel consumption. A single tire under-inflated by 56 kpa (8 psi) can increase fuel consumption by?", "25 per cent", "15 per cent", "Seven per cent", "Four per cent", "Four per cent"));
listS.add(new ModelClass("You are not allowed to park a vehicle within _____________ of a pedestrian corridor", "9 metres", "3 metres", "30 metres", "15 metres", "15 metres"));
listS.add(new ModelClass("What class licence permits the holder to operate a motorcycle?", "Class 4", "Class 1", "Class 6", "Class 5", "Class 6"));
listS.add(new ModelClass("Seatbelts must be worn by all drivers and passengers:", "Only when driving in the city", "Only when carrying passengers", "All the time unless exempted by law", "Only when driving on the highway", "All the time unless exempted by law"));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, DashboardActivity.class);
startActivity(intent);
}
}, 1500);
}
I try to use the list that is created above but the list always remains empty. I also tried to use getter but without success. After debugging I realized that the splash activity was not running at all
This is my AndroidManifest-
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/mainscreenlogo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.QuizTheoryApplication"
tools:targetApi="31">
<activity
android:name=".example.QuizTheoryApplication.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".example.QuizTheoryApplication.DashboardActivity"
android:label="Preferences"
android:exported="true">
<intent-filter>
<action android:name="com.iphonik.chameleon.AppPreferenceActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".example.QuizTheoryApplication.SplashActivity"
android:label="Preferences"
android:exported="true">
<intent-filter>
<action android:name="com.iphonik.chameleon.AppPreferenceActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
The problem is that my Splash Activity doesn't run and of course, the list is always empty. I've tried to add in Dashboard activity but without success -
new Handler().postDelayed(new Runnable()
{ @Override public void run()
{ Intent intent = new Intent(DashboardActivity.this, SplashActivity.class); startActivity(intent); } }, 1500);
I solved my problem by implementing of a singleton pattern in SplashActivity:
private static SplashActivity instance;
public static SplashActivity getInstance() {
if (instance== null) {
synchronized(SplashActivity.class) {
if (instance == null)
instance = new SplashActivity();
}
}
// Return the instance
return instance;
}