简体   繁体   中英

Android: a static TextView in a parent Activity and two child Activity classes that manipulate it

I have 3 Activity classes, one parent and two children. The parent class has a static TextView, which I want the child classes to make use of.

My idea was that I would use this TextView as a shopping cart counter, and because it's static, I wouldn't have to worry about refreshing the child classes whenever the user switches between activities because there would be only one instance of the TextView to refer to.

Some pseudocode to help illustrate:

public class ParentActivity extends AppCompatActivity {
    protected static TextView cartCount;
    ...
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ...
        cartCount = (TextView) actionLayout.findViewById(R.id.cart_count);
        cartCount.setText(cartObject.getTotalAmountOfItems());
        ...
    }
}

public class HomeActivity extends ParentActivity {
    ...
    onItemClick
        cartCount.setText("1") // user adds one item to the cart
}

public class CartActivity extends ParentActivity { // lists items currently in shopping cart
    ...
    onItemClick
        cartCount.setText("2") // user realizes they want two of the selected item
}

This almost works, except that when I return to HomeActivity (launchMode=singleTop) by clicking the back button, the counter is what it was before navigating to CartActivity, and it remains that way even when attempting to add or remove items in the shopping cart. A refresh of HomeActivity is still required in order to get it to start reflecting the correct number of items again, which can be done by rotating the screen.

It's almost as if the static TextView becomes fixated on CartActivity, so the TextView I'm seeing in HomeActivity is no longer a true representation of the TextView that I declared in ParentActivity. Can anyone elaborate on what's happening behind the scenes in this scenario?

Static views are not a very good idea. They can cause memory leaks and crashes if not used with extra care in the rare cases you use them. Your case does not sound like one of the cases I would use a static TextView. Instead it would probably be better to use some kind of shared preference to hold data which would be common among your activities.

See more info about how to do it in: http://developer.android.com/training/basics/data-storage/shared-preferences.html

I also suggest not to use static view, not at all a solution. I would suggest two solutions for your problem: 1. Application class: if you know how to implement it then just create a variable in your application class to hold the Cart data. And whenever your Home activity comes in front ie onStart method (please check your business casses) 2. SharedPreference: you can create shared preference and save the cart data in this and again in onStart of your main activity you can clear the preference.

Confused on the reasoning behind the cart count, you want to add 1 to the cart count?

Maybe passing in the value of setText through a bundle when you go back to the home so that the value is stored. You are using startActivity when you navigate between activities right?

public class HomeActivity
{

public final String EXTRA_cartCount = "HomeActivity.cartCount"
...

@Override
    public void onBackPressed()
    {
        Intent i = new Intent(this,ParentActvity.class);
        i.putExtra(EXTRA_cartCount,cartCount);
        startActivity(i);

    }

}
Also in cardActivity

now in

public class CardActivity {

public final String EXTRA_cartCount = "CardActivity.cartCount"
...

@Override
    public void onBackPressed()
    {
        Intent i = new Intent(this,ParentActvity.class);
        i.putExtra(EXTRA_cartCount,cartCount);
        startActivity(i);

    }

}

ParentActivity

public class ParentActvity {

...

if(getIntent().getClass().equals(HomeActivity.class))
        {
            getIntent().getExtras().get("HomeActivity.cartCount");
        }
        else
        {
            getIntent().getExtras().get("CartActivity.cartCount");
        }



}

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