简体   繁体   中英

How do i clear textviews/edittexts from one activity in another activity?

I'm pretty new to developing in Xamarin and I have 'some' background with WinForms using C# and Android Studio using Java.

The case is this, I already have my app 99% done. The app consists of two activities. In Activity1 the user fills some fields with text that's needed to be sent to Activity2 so the user can add extra data by filling out more text fields and once that's done and everything is sent to a Database Table the Activity2 executes Finish(); and then makes the user return to Activity1, which i've already done.

Now, what i want is so that when the Activity2 closes and the user is sent back to Activity1 , i want all textfields in ACtivity1 to be cleared out but only when everything in the Activity2 is done because i don't want to set NoHistory to true for the Activity1 in case the user needs to go back and correct something.

I've tried creating a method either in Activity1 that set those text fields to null, and it works by setting up a button in that same activity. I've also tried to call it in Activity2 with:

Activity1 activity1 = new Activity1 activity.method();

Or by setting the textviews/edittexts in Activity2 like I do in Activity1 :

setting a {get; set;} calling the textview with a FindViewbyId And then: textview.text = "" (or = null)

I always get: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object . For both options when the app tries to clear or set the textfields to null in Activity1 when it is executed in Activity2 .

Can someone explain to me how to just clear all textviews from one activity in another activity?

Beforehand, thank you very much for taking your time to read/help this poor soul.

Activity2 needs to provide information that Activity1 uses to decide whether to clear fields.

The principle is to DEFER MAKING CHANGES IN Activity1 until Activity1 is the visible activity. That is, AFTER returning to Activity1.


Given lack of code in your question, here is one simple approach. It might not be the best approach in your situation, but it hopefully gives you some guidance.

IF Activity2 is a "singleton" - there are never "two different Activity2s at the same time", THEN it is valid to use a static member of Activity2 :

class Activity1 : Activity
{
  protected void SomeMethodThatExecutesWhenActivityBecomesVisible()
  {
    if (Activity2.IsComplete)
    {
      .. // clear text fields
    }
  }
}

class Activity2 : Activity
{
  // IMPORTANT: "static".
  public static bool IsComplete;

  protected void SomeMethodThatExecutesWhenActivityBecomesVisible()
  {
    // Because `IsComplete` is static, be sure to clear it
    // each time Activity2 is used.
    IsComplete = false;
  }

  private void MyFinishMethod()
  {
    ... // logic that decides whether user "is done with Activity2" ...
    IsComplete = ... true or false as appropriate ...;
}

(I haven't worked with Xamarin.Android code in a while; put in the appropriate method name for SomeMethodThatExecutesWhenActivityBecomesVisible .)

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