0

my problem is this:

I make a toast "welcome" in the Activity Main for the first time that you open the home, and it's ok,but when another page to return to the home via the back button, how can I make the toast "welcome" does not appear anymore?

the code of main activity is:

public class MyActivity extends Activity {
/**
 * Called when the activity is first created.
 */
MyActivity actvi1;
int cont=0;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);





    Button btnabout=(Button)findViewById(R.id.about);

    //click

    btnabout.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // definisco l'intenzione di aprire l'Activity "aboutme.java"

            Intent aboutmejava= new Intent(MyActivity.this,aboutme.class);

            startActivity(aboutmejava);
        }

    }

    );


    //toast

       Toast toast = Toast.makeText(getApplicationContext(),
               "Welcome", Toast.LENGTH_SHORT);
       toast.setGravity(Gravity.CENTER_HORIZONTAL, 20, 0);
       toast.show();

code of aboutme.java

 protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aboutme);

    Button btnback=(Button)findViewById(R.id.scritta);

    btnback.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            Intent main = new Intent(aboutme.this,MyActivity.class);

            startActivity(main);
2
  • so you want to make this toast appears only the first time you run your application?
    – Blackbelt
    Oct 3, 2014 at 18:29
  • yes, exactly when I go back to the home should not appear. Only the first time
    – spyker10
    Oct 3, 2014 at 18:32

4 Answers 4

2

If you want it to only show the very first time the application runs, put a boolean flag in SharedPreferences and check here. There are tons of examples but here is one

If you want it to show *every time the Activity is first run when the app starts, simply replace your onClick() code with onBackPressed(). This way it won't start a new instance of your MyActivity and since the Toast code is in onCreate() and not onResume(), it won't run when you go back by clicking the back button.

SharedPreferences

1

This works.

    SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
    int show = sp.getInt("firstlaunch", 0);
    if(show == 0) {
        Toast.makeText(getApplicationContext(), "WELCOME", Toast.LENGTH_LONG).show();
        sp.edit().putInt("firstlaunch", 1).apply();
    }

Place it in your home activities onCreate method.'

A shared preference is a "setting" of sort. It's a xml file that is loaded which contains all of your settings. When we first run "sp.getInt" you can see that i have a 0 after the "key - fistlaunch". The 0 is specifying what to give our Int SHOW if it can't find any shared preference with that key. Next if the int show is equal to 0 we run our Toast and then change the shared preference value to 1 so next time you run it doesn't show...

9
  • 1
    At least inform the OP what that code actually does instead of claiming "it works" Oct 3, 2014 at 18:37
  • Java is pretty self explanatory. BUT i will, for you.
    – Seth
    Oct 3, 2014 at 18:38
  • 1
    Not for me. I know what it does. But the OP didn't ask for shared preferences. So if you're going to give a solution with it at least explain why. And also SharedPreferences is not Java as you're saying. Oct 3, 2014 at 18:39
  • SharedPreference is a Java class that is a part of the android API. Nothing is just "java" unless your talking about the raw code. It's composed of a toolset of classes (and native methods) that create what we know as the SDK. which i'm referring to of course, the Android SDK.
    – Seth
    Oct 3, 2014 at 18:41
  • in this case I run the toast just the first time I launch the app, but if I close and reopen the beginning do not execute the toast.
    – spyker10
    Oct 4, 2014 at 13:21
0
   boolean b = false;
if(!b) {
Toast toast = Toast.makeText(getApplicationContext(),
        "Welcome", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 20, 0);
toast.show();
b = true
}

kind of like that?

1
  • I mean putting the boolean b = false in the beginning of the class(think thats called global, right?), should have mentioned that :P Oct 4, 2014 at 9:35
0

Are you another instance of MainActivity? In order to return to your Main activity you should send an Intent with the flag FLAG_ACTIVITY_CLEAR_TOP (or set android:launchMode="singleTop" in launchMode in AndroidManifest.xml)

 Intent main = new Intent(aboutme.this,MyActivity.class);
 main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(main);

By doing so, your main activity will be restored, instead of creating a new instance of MainActivity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.