每次单击按钮来回切换活动时,我都试图将变量计数的值增加一。但是,每次我单击时,计数值仍然相同。有人可以帮忙吗?
我正在尝试放置一些生命周期 android 代码,但它仍然没有任何区别。
package com.darayuth.learning;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.darayuth.learning";
public static final String TAG = "MainActivity";
public static final String value = "value";
private int count = 0;
private TextView textView;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(value, count);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
count = savedInstanceState.getInt(value);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView2);
Button btn = findViewById(R.id.button);
btn.setOnClickListener((View v)->{
sendMessage();
});
}
public void sendMessage(){
count = count + 1;
Log.i(TAG, "value: " + count);
Intent intent = new Intent(this, Main2Activity.class);
EditText editText = findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message );
startActivity(intent);
}
}
</LinearLayout>
慕容森
相关分类