简体   繁体   English

Android:父Activity和两个子Activity类中的一个静态TextView

[英]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. 我有3个活动班,一个父母和两个孩子。 The parent class has a static TextView, which I want the child classes to make use of. 父类有一个静态的TextView,我希望子类可以使用它。

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. 我的想法是将这个TextView用作购物车计数器,并且由于它是静态的,因此我不必担心每当用户在活动之间切换时刷新子类,因为只有TextView的一个实例可以引用至。

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. 这几乎可以用,除了当我通过单击“ 后退”按钮返回HomeActivity(launchMode = singleTop)时,计数器是导航到CartActivity之前的计数器,即使尝试添加或删除购物车中的商品,计数器也保持这种状态。 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. 仍然需要刷新HomeActivity才能使其再次开始反映正确数量的项目,这可以通过旋转屏幕来完成。

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. 几乎就像静态TextView固定在CartActivity上一样,因此在HomeActivity中看到的TextView不再是我在ParentActivity中声明的TextView的真实表示。 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. 您的情况听起来不像是我将使用静态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 在以下网址中查看有关如何操作的更多信息: 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. 我将为您的问题提供两种解决方案:1.应用程序类:如果您知道如何实现,则只需在应用程序类中创建一个变量来保存Cart数据。 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. 并且无论何时您的Home活动都位于前面,即onStart方法(请检查您的业务案例)2. SharedPreference:您可以创建共享首选项并在其中保存购物车数据,然后在主要活动的onStart中再次清除首选项。

Confused on the reasoning behind the cart count, you want to add 1 to the cart count? 对购物车数量背后的推理感到困惑,您想在购物车数量上加1吗?

Maybe passing in the value of setText through a bundle when you go back to the home so that the value is stored. 回到家时,也许通过捆绑包传递setText的值,以便存储该值。 You are using startActivity when you navigate between activities right? 当您在活动之间导航时,您正在使用startActivity吗?

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 { 公共类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 { 公共类ParentActvity {

...

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



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM