开发者

Multiple activity instances problem

In my application I have an activity class A that has a listview with a cursor adapter. From A I can go to the activity B, by pressing a button. From B I can go back to A by pressing a button (not by pressing the BACK button). This means that a new instance of the A activity is created.

From this point, if I press the BACK key, the current A activity is destroyed and B is popped. And if I press BACk again the initial A activity is popped. I hope it is clear.

My pr开发者_开发技巧oblem is that when the second A activity is destroyed, the database connection is reseted, in a static manner. So in the end, when the initial A activity is displayed, the listview will be empty.

My question is: should I try to have a single instance for the A activities, or shoud I change the database connection (to link it with the activity instance)?

Thanks a lot Gratzi


First Of All In class A which is carrying your ListView . on clicking any Listview call the startActivity method for the Class B Activity without calling any finish(). I hope which is you are already doing. Now in the Second Activity The button (Not the Back Button) you are using for calling Activity A . in its clickListener for calling Activity A dont call the startActivity(intentForA) instead call the finish(); for ending the Activity B. this will resume the A activity which is paused..

I hope this will help


You will need to create 3 Activities rather than 2. Have a MAIN activity that does not really display anything.

So You have Activity A that is your main activity that can handle the connection to the DB etc.

Then Activity B and C can be the A and B that you have used.

Activity A (Main activity) can have a static instance of itself so you can refernce it's Variables etc -OR- you can pass data from one activity to the other using Intent.put, etc.

I prefer the global static instance way as I'm a little old school on Java.

Edit: Forgot to mention, to handle the 'closing' of the app, either Activity B or C must also close Activity.

public class ActivityA extends Activity {
    ActivityA act_a_instance;
    public int some_integer = 22;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        act_a_instance = this;//Now you can reference this Activity outside
        //Your creation stuff etc
    }
} 


public class ActivityB extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
              //Your creation stuff etc

              //Reference stuff from ActivityA like so :
              int temp_integer = ActivityA.act_a_instance.some_integer;
        }
       @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.options_back:
                startActivity(new Intent(this, ActivityC.class));
                break;
            }
        }

    @Override
    protected void onStop() {
        finish();
        super.onStop();
    }
}

public class ActivityB extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {

            //Your creation stuff etc
        }
       @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.options_back:
                startActivity(new Intent(this, ActivityB.class));
                break;
            }
        }

    @Override
    protected void onStop() {
        finish();
        super.onStop();
    }
}


Use below code hope this will solve your problem

Intent i = new Intent(B.this, A.class); 
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(i);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜