1

in my project there are a few classes. Let's call them A, B, C, D. I discovered 2 problems: e.g. i go from "A" class to "B" then to "C". In "B" class there is button and a counter. When i first push a btn in class "B", a counter is started and e.g. when the counter is at Number 6, class "C" opens. When i go back now from class "C" to class "B" (by clicking the back btn of my phone), counter is at Number 6, and not 0.

Second problem: I go from "A" to "B", then to "C" then back to "B" then to "C" and so on. When i push the back btn in class "C" i should go back to class "B" then to "A" then exit. But here is what happens: I go back to class "B" then i go back to class "C" then to class "B" then to class "A", so it goes back through all the classes that was opened before.

What's more, my exit button (Main.this.finish();) on the firworks only at the beginning. If i go through some classes, it behaves like the back button, making quitting the application impossible.

I don't if these problems are connected or not, but i would like to hear your opinions about them.

Thanks.

Edit: I have read your comments and suggested webpages. I would like to be more specific now. For now, let's talk about three activites. Start.java, CustomizeDialog.java and Highscores.java. When user opens Start.java (by clicking a button in Main.java), a counter is started. Then after 3 seconds the user is automatically forwarded to CustomizeDialog.java to write his name. After clicking OK on that dialog, he is forwarded to HighScores.java. Now if he clicks the back button, he goes back to Start.java. Here the counter is at where it was left before (i know it is Android's behaviour).

So Main -> Start -> CustomizeDialog -> Higshcores -> click back -> Start (CustomizeDialog is not an activity, but a dialog this is why it is omitted when going back)

I tried this in the manifest, but does nothing:

<activity
    android:name=".Start"
    android:label="@string/app_name"
    android:finishOnTaskLaunch="true">
</activity>

This is how i use the counter:

In a button listener in the onCreate method:

counter = new MyCount(3000,1000);
if (started == false)
{
  counter.start();
  started = true;
}

Outside the onCreate method:

public class MyCount extends CountDownTimer{
        public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);

        }

 public void onFinish() {
//filewrite
CustomizeDialog customizeDialog = new CustomizeDialog(Start.this);
        customizeDialog.show();
    };

 @Override
        public void onTick(long millisUntilFinished) {
        tv.setText("" + millisUntilFinished/1000);
    }

And OK button listener in CustomizeDialog:

Intent intenthighscores = new Intent(mActivity, Highscores.class);
        mActivity.startActivity(intenthighscores);
//Activity mActivity; is declared at the beginning of the dialog

I removed the exit button as i read it is unnecessary, i will deal with it later as with the second problem, if that will still exist since it is related to the first problem.

erdomester
  • 11,789
  • 32
  • 132
  • 234

2 Answers2

1
  1. Activity B doesn't recreating after activity C was finished. If you want to reset the counter, do it in onResume() method of activity B.

  2. I don't know what is problem. Do you override onBackPressed() or onKeyDown()?

  3. Main.this.finish() finishing only one activity - activity Main. It doesn't finish application.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
1

As for your first two problems: this is normal behaviour, see this article for a detailed explanation about back stack (also note that "class" is a general expression, the Android application components you are talking about are called Activities).

About exiting the application, please read this question and the first answer.

Community
  • 1
  • 1
molnarm
  • 9,856
  • 2
  • 42
  • 60