I'm new to Android programming, so I'm probably missing something trivial here.
The goal is to create a prototype of a digital signage app. Right now I've created three activities; MainActivity
has a method which switches to the second activity after a certain period of time. The same method is then called from the second activity to third, and from third back to main.
There are two problems though: first, is it ok to create new Intent every time the app switches between the activities? As I mentioned, I started learning Android recently, so please don't be mad if this is a super dumb question.
Second: the app launches itself after I've hit home button in my emulator even though I'm calling finish();
after the startActivity(intent);
Here is the method in the MainActivity
:
public void switchActivities(final Class<?> classObject) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getApplicationContext(), classObject);
startActivity(intent);
finish();
}
}, 1000);
}
The activities Second and Third extend MainActivity and call the switchActivities method:
switchActivities(Third.class);
(from Second to Third).
Thanks in advance!
UPDATED:
I added public boolean isRunning = true;
to my MainActivity and
if(isRunning) startActivity(intent);
to the switchActivities method;
I also added a method
@Override
protected void onPause(){
super.onPause();
isRunning = false;
}
as advised here.
Although finish();
should have cleared the method stack, the app didn't close after pressing Home button but went to previous activity instead, so I added this line to every activity in the AndroidManifest file:
android:noHistory="true"
as advised here.
Sorry for not upvoting helpful answers, I don't have enough reputation yet.