简体   繁体   中英

How do I start the an activity when user doesn't use it anymore

I have two activities in my app, first activity leads to second and the second leads back to the first. In the second one he can record and message and leave his name. In case the user only uses the second activity and just walks away I want that it jumps automatically to the first one after 20 seconds. Do I have to use a Handler for that? Because if I use

 Handler handler = new Handler();
            Runnable r = new Runnable() {
                public void run() {
                  // my code 
                }
            };
            handler.postDelayed(r,20000);
        }
    });

It will change the activity independently from the user input? Any ideas? Thank you

You need to check if the user left his name in run(). If there is a name, do not execute the code that starts the other activity.

Handler handler = new Handler();
        Runnable r = new Runnable() {
            public void run() {
                if (textViewName.getText().toString().length() == 0) {
                    // my code 
                }
            }
        };
        handler.postDelayed(r,20000);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM