【问题标题】:Why my aplication call the OnCreate's event every i call an activity?为什么我的应用程序每次调用活动时都会调用 OnCreate 事件?
【发布时间】:2014-12-29 18:57:14
【问题描述】:

我有问题。

由于其他活动,我尝试在列表视图中添加一个值,所以我有 2 个活动

  • 数字 1 有一个按钮(用于调用活动 2)并有一个列表视图

  • 另一个活动有一个 2 编辑文本和一个按钮,用于调用活动 1(此处在 putextras 中传递 2 个值)

所以当我调用活动 2 并返回事件 Oncreate 再次调用时,有人知道为什么吗?

我的代码活动 1

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;



public class MainActivity extends ActionBarActivity {

    private ArrayList<User> arrayOfUsers;
    private UsersAdapter adapter;
    private ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.lvItems);
        arrayOfUsers = new ArrayList<User>();
        adapter = new UsersAdapter(this, arrayOfUsers);
        listView.setAdapter(adapter);
        Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Bundle extras = getIntent().getExtras();
        if(extras != null)
        {
            if( extras.getString("ename") != null && extras.getString("ehome") != null) {

                User newUser = new User(extras.getString("ename"), extras.getString("ehome"));
                adapter.add(newUser);
            }
        }
        listView.setAdapter(adapter);
        Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void clicbtn(View v){
        Intent i = new Intent(MainActivity.this,MainActivity2.class);
        startActivity(i);
    }

}

我的代码活动 2

public class MainActivity2 extends ActionBarActivity {

private EditText ename;
private EditText ehome;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity2);
    ename = (EditText) findViewById(R.id.editText);
    ehome = (EditText) findViewById(R.id.editText2);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void accion(View v){

    Intent i = new Intent(MainActivity2.this, MainActivity.class);
    //i.putExtra("ename",ename.getText().toString());
    //i.putExtra("ehome",ehome.getText().toString());
    startActivity(i);
}

}

【问题讨论】:

    标签: android listview events android-intent android-activity


    【解决方案1】:

    onCreate() 被调用是因为您再次将其作为该行的新活动启动

    Intent i = new Intent(MainActivity2.this, MainActivity.class);
    //i.putExtra("ename",ename.getText().toString());
    //i.putExtra("ehome",ehome.getText().toString());
    startActivity(i);
    

    如果您想将值发送到以前的活动,请使用startActivityForResult

    这个 SO question 可以帮助你实现这个How to manage `startActivityForResult` on Android?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2015-01-25
      相关资源
      最近更新 更多