1

I want to fill in all properties of object myRule based class MRule, I create a several activities to do it, an Activity to pass name and enabled, an Activity to pass onlyNumberList, exceptNumberList, etc... and use Back and Next button to switch different Activity.

Because Intent.putExtra can't pass object, so I plan to create a public static object staticMRule as shared var among with activities, is it OK?

And , is there the more better way?

Public static  MRule  staticMRule; 



public class MRule {
    public int ruleID;
    public String name;
    public Boolean enabled;
    public IncomingType incomingType;
    public List<String> onlyNumberList;
    public List<String> exceptNumberList;
    public List<String> receiverNumberList;  //短信转发到的号码

    public MRule(){ 
        onlyNumberList=new ArrayList<String>();
        exceptNumberList=new ArrayList<String>();
        receiverNumberList=new ArrayList<String>();
    }   
}


public class activity1  extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);       

        Button btnNext = (Button) findViewById(R.id.btnClose);
        btnNext.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                  staticMRule.name="a"; 
            }
        }); 

    }
}

public class activity2  extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);       

        Button btnNext = (Button) findViewById(R.id.btnClose);
        btnNext.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                  staticMRule.enabled=false;
            }
        }); 

    }
}
HelloCW
  • 843
  • 22
  • 125
  • 310

3 Answers3

1

Application Class

Application class is the base class where you can maintain global variables. One can override Application class by extending his own class from it and specifying that class in AndroidManifest.xml’s tag.

Application class exists through out the app life cycle. Creating a static singletons can serve the purpose instead of extending and overriding Application class.

Following code you can use

public class YourApplication extends Application 
{     
     private int result = 0;

     public int getResult() {
          return result;
     }

     public void setResult(int result) {
         this.result= result;
     }
}

To access it,

YourApplication app = ((YourApplication)this.getApplication());
app.setResult(100);
//...
int result = app.getResult();

References

Community
  • 1
  • 1
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
0

You can make class which implements parcelable and then you can easily pass Object of that class by using Intent.putExtra(key,ObjOfYourParcelableClass)

Mitul Gedeeya
  • 886
  • 1
  • 10
  • 20
0

Another option that is more elegant is to use an injection framework with a singleton. Have a look at RoboGuice or Dagger.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
Snicolas
  • 37,840
  • 15
  • 114
  • 173