简体   繁体   English

如何清除背面按下的片段中的文本字段?

[英]How to clear the text fields in fragments on back pressed?

I am facing the issue of clearing the text view on back pressed in the fragment. 我面临的问题是清除片段中按下的文本视图。 When I set the text to textview in Fragment A and then clicked submit button to move the next fragment B. After that when i back pressed in Fragment B to go back to the previous fragment B. In that Fragment A the textview text is not cleared how to clear the text in textview. 当我在片段A中将文本设置为textview时,然后单击“提交”按钮以移动下一个片段B。之后,当我在片段B中按回以返回到先前的片段B时。在该片段A中,不会清除textview文本如何清除textview中的文本。 Please help me how to solve this. 请帮我解决这个问题。

The below code is for replacing the fragment from Fragment A to B. 以下代码用于将片段从片段A替换为片段B。

Bundle args = new Bundle();
args.putString("fromDate",fromDate);
args.putString("toDate",toDate);
args.putInt("account",accountPosition);
//     ldf.setArguments(args);

//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.fragment_container, ldf).commit();
getFragmentManager().popBackStack(Fragment_Retailer_Home_Main.class.getSimpleName(),
FragmentManager.POP_BACK_STACK_INCLUSIVE);


Fragment_Retailer_account_Statements fragment = new  Fragment_Retailer_account_Statements();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragment.setArguments(args);
fragmentTransaction.replace(R.id.fragment_container, fragment,"Fragment_Retailer_account_Statement");
//         fragmentTransaction.addToBackStack(Fragment_Retail_Enter_Date.class.getSimpleName() );
fragmentTransaction.commit();

use onResume() method 使用onResume()方法

@Override
public void onResume() {
    super.onResume();

    /*code to set text to null 
      eg : txtBoxName.setText(""); or
           txtBoxName.clearComposingText(); */

 }
}

You can consider not adding the fragment to backStack, and recreating a new one on back pressed. 您可以考虑不将片段添加到backStack,并在按下后重新创建一个新片段。 You can add a flag to your activity, and check the flag inside OnResume in Fragment you would like to get cleared. 您可以在活动中添加一个标志,然后在要清除的Fragment中的OnResume中检查该标志。

Fragment A: 片段A:

getFragmentManager().beginTransaction()
                    .replace(R.id.frag, Fragment B)
                    .addToBackStack(null)
                    .commit();

From Fragment B(back press) : 从片段B(回压):

getFragmentManager().popBackStack();

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

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