user1114971
user1114971

Reputation: 1

How to show a dialog by first app launching

Is it possible to create a dialog, who is only showing, when the app is started for the first time? And when it is possible, how do I make it?

thanks

Upvotes: 0

Views: 152

Answers (2)

Bill Gary
Bill Gary

Reputation: 3005

try this, no need for a database or filestream, etc.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main):

    SharedPreferences prefs = getSharedPreferences(filename, 0);
    boolean runOnce = prefs.getBoolean("ranOnce", false);
            if (runOnce == false){
                 //dialog code here
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("ranOnce", true);
            editor.commit();
            }
     //normal onCreate code here
}

It sets up a SharedPreference that will be false to start. If it's false it will run the dialog code then set itself to true. Once it's true, it won't run the dialog code the next time the app starts.

Upvotes: 6

Irakli dd
Irakli dd

Reputation: 56

You can save file in cache directory when run this dialog (or for example toast message)

// WRITE

File f_cache = (activity_name).this.getCacheDir();
f_cache_path = f_cache.getAbsolutePath();
OutputStream title_stream = null;
File title_forsave = new File(f_cache_path + File.separator + "info.txt");
title_stream = new FileOutputStream(title_forsave);
title_stream.flush();
title_stream.close();

// READ

FileInputStream title_in = new FileInputStream(f_cache_path + File.separator + "info.txt");

// AND ALL

//read
FileInputStream title_in = new FileInputStream(f_cache_path + File.separator + "info.txt");

if (title_in != null) {
title_in.close();
} else {

YOUR DIALOG FUNCTION (OR OTHER)

// write

File f_cache = (activity_name).this.getCacheDir();
f_cache_path = f_cache.getAbsolutePath();
OutputStream title_stream = null;
File title_forsave = new File(f_cache_path + File.separator + "info.txt");
title_stream = new FileOutputStream(title_forsave);
title_stream.flush();
title_stream.close();

}
  • user can clear cache and view your function again - when he want.

Upvotes: -2

Related Questions