1

I want to create a child activity which will be opened on parent activity object click. For a better understanding see screenshots of redbus application:

enter image description here enter image description here enter image description here

In the first screen user clicks on Enter City (highlighted with red) then goes to second screen where user selects on city by searching with the name of the city. Then after clicking on city user is taken back to screen 1 with selected city and also it doesn't affect the previous data of screen 1.

I tried for the same, you can see the code:

UserDetails.java

@Override
public void onClick(View v) {
    switch (v.getId()) {

        case R.id.btnCancel:
            CancelAll();
            break;
        case R.id.defenceArea:
            DefenceAreaSelection();
            break;

        default:
    }
}

private void DefenceAreaSelection() {

    isSelection = true;
    ArrayList<Integer> IdList = new ArrayList<>();
    for (int i=0;i<mDefenceAreaModel.size();i++){
        IdList.add(mDefenceAreaModel.get(i).getDefenseAreaID());
    }
    Intent intent = new Intent(UserDetails.this, ListSelection.class);
    intent.putExtra("names",mDefenceAreaNames);
    intent.putExtra("ID",IdList);

    //TODO send 2 array for id and names to ease the process
    startActivityForResult(intent,requestCodeDefenceArea);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == requestCodeDefenceArea){
        Log.e("defe","got it");
    }
}

ListSelection.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_select_screen);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);

    try {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    } catch (NullPointerException e) {
        e.printStackTrace();
        //Log.e("exception",e.getMessage());
    }
     ArrayList<String> listNames  = (ArrayList<String>) getIntent().getSerializableExtra("names");
     ArrayList<Integer> listId  = (ArrayList<Integer>) getIntent().getSerializableExtra("ID");
    list = (ListView)findViewById(R.id.listNames);

    ArrayAdapter adapter = new ArrayAdapter(ListSelection.this,android.R.layout.simple_dropdown_item_1line,listNames);
    list.setAdapter(adapter);

    //TODO  onitem click set the result and go back to parent activity
}

But the problem I am getting is after selection of area it goes back to previous screen but the previous data in screen 1 doesn't exist anymore that means apart from on Activity Result it is also calling onCreate() method.

Bryan
  • 14,756
  • 10
  • 70
  • 125
Mithilesh Izardar
  • 2,117
  • 1
  • 13
  • 24

2 Answers2

1

The best thing to do is use EventBus library. I have a demo app in which you can add items to RecyclerView from anywhere within the app using EventBus. Instead of a RecyclerView you can insert it into the TextView. Here is the link to the repo:

https://github.com/code-crusher/android-demos/tree/master/EventBusDemo

Please also do read the article for better understanding and reference:

https://medium.com/@code_crusher/eventbus-for-android

Hope it helps. Happy coding :)

xblack
  • 581
  • 2
  • 8
0

The problem is that you're not saving the view state!

If you're using custom views, their content isn't saved by default, especially if you're overwriting onSaveInstanceState(), without calling the super.

There's many ways you can solve the problem, which of the best is to persist the content within the view, which of more you can read at this StackOverflow answer

But if you want to go the simple way, you can simply within onSaveInstanceState() in your activity save the nesaccary data in a Bundle and set the previous data in onRestoreInstanceState()

Community
  • 1
  • 1
Pasi Matalamäki
  • 1,843
  • 17
  • 14