0
  • I am creating an app that requires some settings to be set first before starting the display activity. I would like the app to check if a preference file (that contains these settings) exists and if it does exist move to a particular activity or remain in the main activity(as the settings activity is my main activity).

  • I tried to use startactivty() on startup, but it shows the settings menu for a fraction of a second before moving on to the display activity.

  • how should I do it?

    I tried the following code:

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);          
            SharedPreferences prefsfile = getSharedPreferences("myfile",0); 
    
            if( prefsfile!=null & Timetablledisplay.flag==0){
                Intent i=new Intent(ProjectBunkitActivity.this,Timetablledisplay.class);
                startActivity(i);
            }
    
user370305
  • 108,599
  • 23
  • 164
  • 151
jaisonDavis
  • 1,407
  • 2
  • 22
  • 36

2 Answers2

2

You should have the LAUNCHER Activity call startActivity() on the new Activity (plus finish()) in onCreate() before calling setContentView() when needed. This way, the new Activity will be launched "behind the scenes" before an attempt at inflating any layout is made.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences prefs = getSharedPreferences("myFile", 0); 

    if(prefs != null && ActivityOne.flag == 0) {
        Intent i = new Intent(this, ActivityOne.class);
        startActivity(i);
    } else {
        Intent i = new Intent(this, ActivityTwo.class);
        startActivity(i);
    }

    finish();
}
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
1

An option for a potential workaround for this problem would be taking a slightly different approach:

Instead of launching into Settings, and then deciding whether or not to go to the Timetablleddisplay activity, you could start in a blank activity that simply decides which activity to launch.

The activity could be something like this:

super.onCreate(savedInstanceState);      
    SharedPreferences prefsfile = getSharedPreferences("myfile",0); 

    if( prefsfile!=null && Timetablledisplay.flag==0){
        Intent i=new Intent(CURRENT.this,Timetablledisplay.class);
        startActivity(i);
        finish();
    }
    else
    {
        Intent i=new Intent(CURRENT.this,ProjectBunkitActivity.class);
        startActivity(i);
        finish();
    }
gkiar
  • 480
  • 1
  • 6
  • 20