简体   繁体   中英

TextView lost after onPause()

I'm learning how to add elements to LayoutParam . I add a TextView element after each click to ```LayoutParam``.

public void send(View v){
        message=edt.getText().toString().trim();
        if(!message.equals("")){
            TextView txt = new TextView(this);
            txt.setText("You: "+message);
            txt.setPadding(10,10,10,10);
            txt.setTextSize(20);
            txt.setTextColor(Color.WHITE);
            txt.setFreezesText(true);
            llayout.addView(txt);
            scroll.fullScroll(View.FOCUS_DOWN);
            edt.setText("");
        }
    }
    

my problem is when i press the Home Button and i open the app again, i don't see any elements. as they have never been.

Are you recalling the send() method in the onResume() method? if not then the data you had will be lost once the activity/fragment resumes. You would need something like this inside onResume():

@Override
public void onResume(){
    super.onResume();
    send(new View());
}

The parameters inside send, will be whatever view you're passing in.

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