0

i having a problem.

I try add a value in a listview since other activity, so i have 2 activities

  • the number 1 have a button (to call the activity 2) and have a listview

  • the other activity hace a 2 edittext and a button where call to activity 1 (here pass the 2 values ina putextras)

SO when i call the activity 2 and i back the event Oncreate is called againg, someone know why?

my code activity 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);
    }

}

my code activity 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);
}

}

1 Answers1

1

onCreate() is getting called because you are again starting it as a new activity from this line

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

If you want to send values to previous activity use startActivityForResult

This SO question can help you implementing this How to manage `startActivityForResult` on Android?

Community
  • 1
  • 1
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57