简体   繁体   中英

Get EditTexts from referenced Activity

in my application I have an Activity A which has a reference to an Activity B. Now I am trying to get all EditTexts from Activity B in Activity A. In Pseudocode the code looks like:

public class A extends Activity{

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    BActivity B = BActivity.safeGetInstance();
    /**
     * Get EditTexts of Activity B here
     */
    }
}

I have no further information about Activity B since it does not belong to my Application. The only thing I know is that BActivity extends Activity. Is it possible to get the Edittexts of B?

Thanks in Advance

Tobi

When you are using activity A (that is launcher), activity B does not exist yet. If you want to get data from activity B, use startActivityForResult method. It gives you back data after you destroyed B. So you cant get something from A when B hasn't created.

Activity B

public class ActivityB extends Activity implements View.OnClickListener {

TextView yourView;
Button startA;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    yourView = (TextView) findViewById(R.id.tvName);

    startA = (Button) findViewById(R.id.start_button);

    startA.setOnClickListener(this);

}


// start A on click
@Override
public void onClick(View v) {
    Intent intent = new Intent(this, ActivityA.class);
    startActivityForResult(intent, 1);
}
/**
 *
 * @param requestCode
 * @param resultCode
 * @param data - resultData from A
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    int dataFromA = data.getIntExtra("result",1);
    if(dataFromA == 0){
        yourView.setFocusable(false);
    }else{
        yourView.setFocusable(true);
    }
}

}

Activity A

public class ActivityA extends Activity implements View.OnClickListener {

EditText etName;
Button btnOK;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etName = (EditText) findViewById(R.id.etName);


    btnOK = (Button) findViewById(R.id.btnOK);
    btnOK.setOnClickListener(this);
}

//finished activity A and return to B with result
@Override
public void onClick(View v) {
    Intent intent = new Intent();
    intent.putExtra("result", 0);
    setResult(RESULT_OK, intent);
    finish();
}

}

There is no reason you should try this.

If the goal is to get the values inside of those EditTexts, you should store them somewhere in a database/preferences or maybe return them in onActivityResult().

Understand that Android gives no guarantees that the memory of other Activities is still valid. Activities get destroyed (and recreated) left and right.

If the goal is to communicate between applications (as you indicate), then this BActivity should make these values available, for example through a content provider.

Forget about solving this at GUI level.

Assume, you are in Activity A, and calling Activity B,

public void startNextActivity() {
  Intent intent = new Intent(this, ActivityB.class);
  startActivityForResult(intent, SOME_UNIQUE_INTEGER_REQUEST_CODE);
}

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    boolean showEditText = data.getBooleanExtra("show", false);
    if (showEditText) {
      // handle your edit text visibility.
    }
}

In Activity B, somewhere when user leaves, (ex: onBackPressed )

Intent intent=new Intent();  
intent.putExtra("show", trueOrFalse);  
setResult(SAME_UNIQUE_INTEGER_REQUEST_CODE_YOU_SPECIFIED,intent);  
finish();//finishing activity  

Now, when finished, onActivityResult will be fired in Activity A. Handle your UI state there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM