How to pass an Activity instance to another activi

2020-06-29 05:15发布

问题:

Hi I have an activity named BaseActivity, which extends Activity. from this i have to go to SettingsActivity which extends PreferenceActivity, on menu button press. To start a AsyncTask, which is in an independent class, i need an instance of BaseActivity. How can i get a BaseActivity instance in the SettingsActivity?

is there any way like, eg:

intent.putExtra("activity_instance",BaseActivity.this);

回答1:

Use getters and setters and make the class they reside as singleton class.

This is a singleton class.Using this class we can share data(ex: int,boolean,activity instance ...etc) all over the class.

public class CommonModelClass 
{
    public static CommonModelClass singletonObject;
    /** A private Constructor prevents any other class from instantiating. */

  private Activity baseActivity; 

    public CommonModelClass() 
    {
        //   Optional Code
    }
    public static synchronized CommonModelClass getSingletonObject() 
    {
        if (singletonObject == null) 
        {
            singletonObject = new CommonModelClass();
        }
        return singletonObject;
    }


    /**
     * used to clear CommonModelClass(SingletonClass) Memory
     */ 
     public void clear()  
      {  
         singletonObject = null;  
      }


    public Object clone() throws CloneNotSupportedException 
    {
        throw new CloneNotSupportedException();
    }

    //getters and setters starts from here.it is used to set and get a value

    public Activity getbaseActivity()
    {
        return baseActivity;
    }

    public void setbaseActivity(Activity baseActivity)
    {
        this.baseActivity = baseActivity;
    }   

}       

In BaseActivity class do like this.

         Class BaseActivity extends Activity{


                @Override
                protected void onCreate(Bundle savedInstanceState) {

                    CommonModelClass commonModelClass = CommonModelClass.getSingletonObject();

    commonModelClass.setbaseActivity(BaseActivity.this);

//after using the BaseActivity instance dont forget to call commonModelClass.clear(); else it wont be garbage collected         


               }

            }

In SettingsActivity do like this

 Class SettingsActivity extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            CommonModelClass commonModelClass = CommonModelClass.getSingletonObject();



            Activity instanceBaseActivity=  commonModelClass.getbaseActivity();;


       }

    }

please give tick if this works



回答2:

Make a static Context in "Base Activity"

public class BaseActivity extends Activity{
     public static Context ctxt;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

      ctxt  = BaseActivity.this
    }
}

and in your "PreferenceActivity" activity use this way

BaseActivity.ctxt


回答3:

You are confusing activities with class objects. The moment activity class is instantiated it obeys all activity life cycle rules, importantly system can kill this activity any time. So you have to design activities in such a way that it shouldn't be dependent on another activity instance at all but only drive the results. you can write a helper class and call it again and again if you want. if not use storages like sdcard or preference or sandbox to store the information and retrieve it from the other activity. If you want to keep some of these information in memory then subclass Application class and keep them at the application level.