為什麼?如題。為什麼Activity開始後OnCreat
方法只能調用一次?
能不能在一個Activity中多次調用OnCreat()
方法?
如果能實現,請給一些示例代碼。謝謝
同問,我也不明白LZ這樣做有什麼意義,不過是可以通過Fragment實現的
Samplecode:
public class MainActivity extends Activity implements OnClickListener {
private Button btPost;
private Bundle state;
private int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
state = savedInstanceState;
btPost = (Button) findViewById(R.id.btPost);
btPost.setOnClickListener(this);
Toast.makeText(getBaseContext(), " " + counter, Toast.LENGTH_LONG)
.show();
}
@Override
public void onClick(View v) {
counter++;
this.onCreate(state);
}
}