Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:43:47+00:00 2026-06-17T08:43:47+00:00

/** * Example Activity to demonstrate the lifecycle callback methods. */ public class ActivityA

  • 0
/**
 * Example Activity to demonstrate the lifecycle callback methods.
 */
public class ActivityA extends Activity {

    private String mActivityName;
    private TextView mStatusView;
    private TextView mStatusAllView;
    private StatusTracker mStatusTracker = StatusTracker.getInstance();
    private AlertDialog alertBox;
    private static int number = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        init();

        String output = getString(R.string.on_create) + number++;
        showAlertDialog(alertBox, output);
        mStatusTracker.setStatus(mActivityName, output);
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_start));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_restart));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_resume));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_pause));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_stop));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_destroy));
        mStatusTracker.clear();
    }

    public void startDialog(View v) {
        Intent intent = new Intent(ActivityA.this, DialogActivity.class);
        startActivity(intent);
    }

    public void startActivityB(View v) {
        Intent intent = new Intent(ActivityA.this, ActivityB.class);
        startActivity(intent);
    }

    public void startActivityC(View v) {
        Intent intent = new Intent(ActivityA.this, ActivityC.class);
        startActivity(intent);
    }

    public void finishActivityA(View v) {
        ActivityA.this.finish();
    }

    private void showAlertDialog(AlertDialog ad, String msg) {
        ad.setTitle("Logging Msg");
        ad.setMessage(msg);
        ad.show();
    }

    private void init() {
        System.out.println("Value = before &&&&&&&&& " + alertBox);
        if (alertBox == null) { // Why always null?
            alertBox = new AlertDialog.Builder(this).create();
            mActivityName = getString(R.string.activity_a);
            mStatusView = (TextView) findViewById(R.id.status_view_a);
            mStatusAllView = (TextView) findViewById(R.id.status_view_all_a);
            Log.e("ActivityA", "Init done **************************************");
            Log.e("ActivityA", "Init done **************************************");
            System.out.println("Value = after &&&&&&&&&&& " + alertBox);
        }
    }
}

Only the very first time the init is called, I set alertBox and I thought the if block should not be executed any other time. But I am wrong, it runs every time init is being called. Why?

I came from Javascript, if I translate the above code into JS (similar), the if block is only evaluated once.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:43:48+00:00Added an answer on June 17, 2026 at 8:43 am

    You should check how your application follows the android activity lifecycle. The onCreate method gets called every time an Activity is created again (even if it existed before). Check if your onDestroy() method gets called. If the answer is yes, you now know how android handles its Activitys. See http://developer.android.com/training/basics/activity-lifecycle/starting.html for an in detail explanation.

    However if the answer is no, there is something wrong on your activity handling. Maybe you recreate the same activity every time the user pushes a button etc. Maybe you finish() the activity somewhere. But this is just wild guessing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Expandable ListView example found on net Activity: public class ExpandableListViewActivity extends
public class ExampleActivity extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private
I have this class: My problem: public class Example extends Activity implements OnClickListener{ DrawView
I have 3 classes in my example: Class A, the main activity. Class A
When my activity is destroyed by OS because of memory (for example when a
For example, I want to write an activity that launches when you return from
Let's have an example like below: package xliiv.sandbox; import android.app.Activity; import android.os.Bundle; import android.util.Log;
I'm trying out the LoaderCursor example in the API Demo: package com.example.android.apis.app; import android.app.Activity;
Example Div: <div class=container> <div class=select1></div> <div class=select2></div> <div class=select3></div> </div> Now I want
I created a settings activity using eclipse. The setting activity creates two example categories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.