草庐IT

java - ActivityTwo.java没有算

coder 2023-12-19 原文

你好,下面是我的源代码;

这是 ActiviyOne.java

package course.labs.activitylab;

        import android.app.Activity;
        import android.app.Fragment;
        import android.content.Intent;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.TextView;

public class ActivityOne
        extends Activity {

    // Use these as keys when you're saving state between reconfigurations
    private static final String RESTART_KEY = "restart";
    private static final String RESUME_KEY = "resume";
    private static final String START_KEY = "start";
    private static final String CREATE_KEY = "create";

    // String for LogCat documentation
    private final static String TAG = "Lab-ActivityOne";

    // Lifecycle counters
    // TODO:
    public Integer mCreate = 0;
    public Integer mRestart = 0;
    public Integer mStart = 0;
    public Integer mResume = 0;
    // Create variables named
    // mCreate, mRestart, mStart and mResume
    // to count calls to onCreate(), onRestart(), onStart() and
    // onResume(). These variables should not be defined as static.
    // You will need to increment these variables' values when their
    // corresponding lifecycle methods get called.
    // TODO: Create variables for each of the TextViews
    TextView mTvCreate;
    TextView mTvRestart;
    TextView mTvStart;
    TextView mTvResume;

    // named mTvCreate, mTvRestart, mTvStart, mTvResume.
    // for displaying the current count of each counter variable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        // TODO: Assign the appropriate TextViews to the TextView variables
        mTvCreate = (TextView) findViewById(R.id.create);
        mTvRestart = (TextView) findViewById(R.id.restart);
        mTvResume = (TextView) findViewById(R.id.resume);
        mTvStart = (TextView) findViewById(R.id.start);
        // Hint: Access the TextView by calling Activity's findViewById()
        // textView1 = (TextView) findViewById(R.id.textView1);

        Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
        launchActivityTwoButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO:
                // Launch Activity Two
                // Hint: use Context's startActivity() method
                // Create an intent stating which Activity you would like to
                // start
                //Intent intent = null;
                Intent StartIntent = new Intent(getApplicationContext(), ActivityTwo.class);
                startActivity(StartIntent);
                // Launch the Activity using the intent
            }
        });

        // Has previous state been saved?
        if(savedInstanceState != null) {
            // TODO:
            // Restore value of counters from saved state
            // Only need 4 lines of code, one for every count variable
            mCreate = savedInstanceState.getInt(CREATE_KEY);
            mStart = savedInstanceState.getInt(START_KEY);
            mRestart = savedInstanceState.getInt(RESTART_KEY);
            mResume = savedInstanceState.getInt(RESUME_KEY);
            //Log.i(TAG, "mCreate " + mCreate + "mResume " + mResume + "mStart " + mStart + "mRestart " + mRestart);
            //super.onSaveInstanceState(savedInstanceState);
        }

        // Emit LogCat message
        Log.i(TAG, "Entered the onCreate() method");
        // TODO:
        mCreate++;
        displayCounts();
        // Update the appropriate count variable
        // Update the user interface via the displayCounts() method
    }

    // Lifecycle callback overrides

    @Override
    public void onStart() {
        super.onStart();
        // Emit LogCat message
        Log.i(TAG, "Entered the onStart() method");
        // TODO:
        mStart++;
        displayCounts();
        // Update the appropriate count variable
        // Update the user interface
    }

    @Override
    public void onResume() {
        super.onResume();
        // Emit LogCat message
        Log.i(TAG, "Entered the onResume() method");
        // TODO:
        mResume++;
        displayCounts();
        // Update the appropriate count variable
        // Update the user interface
    }

    @Override
    public void onPause() {
        super.onPause();
        // Emit LogCat message
        Log.i(TAG, "Entered the onPause() method");
    }

    @Override
    public void onStop() {
        super.onStop();
        // Emit LogCat message
        Log.i(TAG, "Entered the onStop() method");
    }

    @Override
    public void onRestart() {
        super.onRestart();
        // Emit LogCat message
        Log.i(TAG, "Entered the onRestart() method");
        // TODO:
        mRestart++;
        displayCounts();
        // Update the appropriate count variable
        // Update the user interface
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Emit LogCat message
        Log.i(TAG, "Entered the onDestroy() method");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // TODO:
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt(CREATE_KEY, mCreate);
        savedInstanceState.putInt(RESTART_KEY, mRestart);
        savedInstanceState.putInt(RESUME_KEY, mResume);
        savedInstanceState.putInt(START_KEY, mStart);
        // Save state information with a collection of key-value pairs
        // 4 lines of code, one for every count variable
    }

    // Updates the displayed counters
    // This method expects that the counters and TextView variables use the
    // names
    // specified above
    public void displayCounts() {
        // TODO - uncomment these lines
        mTvCreate.setText("onCreate() calls: " + mCreate);
        mTvStart.setText("onStart() calls: " + mStart);
        mTvResume.setText("onResume() calls: " + mResume);
        mTvRestart.setText("onRestart() calls: " + mRestart);
    }
}

这是ActivityTwo.java

    package course.labs.activitylab;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

public class ActivityTwo
        extends Activity {

    // Use these as keys when you're saving state between reconfigurations
    private static final String RESTART_KEY = "restart";
    private static final String RESUME_KEY = "resume";
    private static final String START_KEY = "start";
    private static final String CREATE_KEY = "create";

    // String for LogCat documentation
    private final static String TAG = "Lab-ActivityTwo";

    // Lifecycle counters
    // TODO:
    public Integer mCreate = 0;
    public Integer mRestart = 0;
    public Integer mStart = 0;
    public Integer mResume = 0;

    // Create variables named   
    // mCreate, mRestart, mStart and mResume    
    // to count calls to onCreate(), onRestart(), onStart() and
    // onResume(). These variables should not be defined as static.
    // You will need to increment these variables' values when their
    // corresponding lifecycle methods get called.
    // TODO: Create variables for each of the TextViews
    TextView mTvCreate;
    TextView mTvRestart;
    TextView mTvStart;
    TextView mTvResume;
    // named  mTvCreate, mTvRestart, mTvStart, mTvResume.
    // for displaying the current count of each counter variable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        // TODO: Assign the appropriate TextViews to the TextView variables
        mTvCreate = (TextView) findViewById(R.id.create);
        mTvRestart = (TextView) findViewById(R.id.restart);
        mTvStart = (TextView) findViewById(R.id.start);
        mTvResume = (TextView) findViewById(R.id.resume);
        // Hint: Access the TextView by calling Activity's findViewById()
        // textView1 = (TextView) findViewById(R.id.textView1);
        Button closeButton = (Button) findViewById(R.id.bClose);
        closeButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO:
                    /*
                    Intent FinishIntent = new Intent(ActivityTwo.this, ActivityOne.class);
                    finish();
                    */
                // This function closes Activity Two
                // Hint: use Context's finish() method
                // ActivityTwo.this.finish();
                // finish();
                //  Intent tmpIntent = new Intent(getApplicationContext(), ActivityOne.class);
                finish();
                //ActivityTwo.this.finish();
            }
        });
    /*
            savedInstanceState.putInt(CREATE_KEY, mCreate);
            savedInstanceState.putInt(RESTART_KEY, mRestart);
            savedInstanceState.putInt(RESUME_KEY, mResume);
            savedInstanceState.putInt(START_KEY, mStart);
    */
        // Has previous state been saved?
        if(savedInstanceState != null) {
            // TODO:
            // Log.i(TAG, "Entered restoring values of counters");
            super.onRestoreInstanceState(savedInstanceState);
            mCreate = savedInstanceState.getInt(CREATE_KEY);
            mStart = savedInstanceState.getInt(START_KEY);
            mRestart = savedInstanceState.getInt(RESTART_KEY);
            mResume = savedInstanceState.getInt(RESUME_KEY);
            //Log.i(TAG, "mCreate " + mCreate + "mResume " + mResume + "mStart " + mStart + "mRestart " + mRestart);
            // Restore value of counters from saved state
            // Only need 4 lines of code, one for every count variable
            // super.onSaveInstanceState(savedInstanceState);
        }
        // Emit LogCat message
        mCreate++;
        Log.i(TAG, "Entered the onCreate() method");
        // TODO:
        displayCounts();
        // Update the appropriate count variable
        // Update the user interface via the displayCounts() method
    }

    // Lifecycle callback methods overrides

    @Override
    public void onStart() {
        super.onStart();
        // Emit LogCat message
        mStart++;
        Log.i(TAG, "Entered the onStart() method");
        // TODO:
        displayCounts();
        // Update the appropriate count variable
        // Update the user interface
    }

    @Override
    public void onResume() {
        super.onResume();
        // Emit LogCat message
        mResume++;
        Log.i(TAG, "Entered the onResume() method");
        // TODO:
        displayCounts();
        // Update the appropriate count variable
        // Update the user interface
    }

    @Override
    public void onPause() {
        super.onPause();
        // Emit LogCat message
        Log.i(TAG, "Entered the onPause() method");
    }

    @Override
    public void onStop() {
        super.onStop();
        // Emit LogCat message
        Log.i(TAG, "Entered the onStop() method");
    }

    @Override
    public void onRestart() {
        super.onRestart();
        // Emit LogCat message
        mRestart++;
        Log.i(TAG, "Entered the onRestart() method");
        // TODO:
        displayCounts();
        // Update the appropriate count variable
        // Update the user interface
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Emit LogCat message
        Log.i(TAG, "Entered the onDestroy() method");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // TODO:
        // Save counter state information with a collection of key-value pairs
        // 4 lines of code, one for every count variable
        Log.i(TAG, "Saving instance state");
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt(CREATE_KEY, mCreate);
        savedInstanceState.putInt(RESTART_KEY, mRestart);
        savedInstanceState.putInt(RESUME_KEY, mResume);
        savedInstanceState.putInt(START_KEY, mStart);
    }

    // Updates the displayed counters
    // This method expects that the counters and TextView variables use the
    // names
    // specified above
    public void displayCounts() {
        // TODO - uncomment these lines
        mTvCreate.setText("onCreate() calls: " + mCreate);
        mTvStart.setText("onStart() calls: " + mStart);
        mTvResume.setText("onResume() calls: " + mResume);
        mTvRestart.setText("onRestart() calls: " + mRestart);
    }
}

我有一个问题。当我运行程序时,我想计算我调用了多少次

onCreate(), onStart(), onResume(), onResume() 它适用于 Activity 一 ,但在 Activity 2 中不起作用。计数器不起作用。

谢谢

最佳答案

1) Activity1 状态保存在堆栈上 -> 当你调用 Activity2 时。

2)当点击返回时,Activity2状态被保存然后被销毁(从堆栈中移除)。

3) Activity1 从堆栈中读取保存的状态。

试试这个 [ Activity 1]:

 launchActivityTwoButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent StartIntent = new Intent(getApplicationContext(), ActivityTwo.class);
    StartIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(StartIntent);
            }
        });

[ Activity 二]:

 closeButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent= new Intent(getApplicationContext(), ActivityOne.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
            }
        });

关于java - ActivityTwo.java没有算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31987719/

有关java - ActivityTwo.java没有算的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  3. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  4. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  5. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  6. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  7. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  8. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  9. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  10. ruby - 没有类方法获取 Ruby 类名 - 2

    如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象

随机推荐