0

I have one activity and inside it I have a Linear layout (lets name it the "main linear layout") and I am adding dynamically created view inside it (text views, linear layouts, edit texts, ...etc).

In the bottom of the screen there are two button (next and back).

If the user clicked on next button I should save the current "main linear layout " in a list for example and then I am generating a new views and I add it inside the "main linear layout" then .

And if the user clicked on back button I should restore the "main linear layout" and show all its views.

I don't know how I can do it.

I hope my description is clear :)

Hadi Al Tinawi
  • 429
  • 7
  • 22

3 Answers3

0

Take a look at onSaveInstance for preserving the view on the main screen when leaving to a different activity or screen orientation.

You can also try and use the buttons to hide the layouts using Visibility instead of trying to navigate through them.

Jeremy
  • 241
  • 4
  • 17
  • I have edit my question, I have one activity and I am reusing the same linear layout. Would onSaveInstance help me in this case? – Hadi Al Tinawi May 03 '17 at 12:46
  • Saving data in onSaveInstanceState and retriving that data onRestoreInstanceState comes into picture only if Activity (that stores the data) is being destroyed due to configuration changes or in low memory condition. Is this what you need? – Pavan May 03 '17 at 13:48
  • I think you may want to look into Fragments using a FragmentTransaction to add (next) or pop (back) to the original view. Take a look at this answer: http://stackoverflow.com/questions/14347588/show-hide-fragment-in-android – Jeremy May 03 '17 at 16:02
0

This may help you. but concrete code need you write it yourself.

public class TestActivity extends Activity{
    Stack<LinearLayout> layouts  = new Stack<>();
    FrameLayout container;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button btnPre = new Button(this);
        Button btnNext = new Button(this);
        btnPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                container.addView(layouts.pop());
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout linearLayout = (LinearLayout) container.getChildAt(0);
                layouts.push(linearLayout);
                container.removeAllViews();
                container.addView(createNextView());
            }
        });
    }
    private LinearLayout createNextView(){
        LinearLayout linearLayout = new LinearLayout(this);
        return linearLayout;
    }
}
Cyrus
  • 8,995
  • 9
  • 31
  • 58
0

Thank you all for your comments.

I haven't test Cyrus answer. I will post my answer as well in case someone has the same problem in Xamarin.Android since I am using it.

I fixed it like this:

I added this in next button click event and previous button click event:

AddQuestionsLinearLayoutToPagesLinearLayoutsList();

And the AddQuestionsLinearLayoutToPagesLinearLayoutsList() body is, of course you can change it the way you want:

private void AddQuestionsLinearLayoutToPagesLinearLayoutsList()
    {
         //Create a new linear layout to add views into it and saved it in _mPagesLinearLayouts list at index _mPageIndex
         LinearLayout savedLinearLayout = new LinearLayout(this);

         //List of _mQuestionsLinearLayout views
         List<View> views = new List<View>();

         //Add all _mQuestionsLinearLayout to views list
         for (int i = 0; i < _mQuestionsLinearLayout.ChildCount; i++)
         {
             views.Add(_mQuestionsLinearLayout.GetChildAt(i));
         }

         //Remove views from main questions linear layout
         _mQuestionsLinearLayout.RemoveAllViews();

         //Add all views from the views list to savedLinearLayout
         for (int i = 0; i < views.Count; i++)
         {
             savedLinearLayout.AddView(views[i]);
         }

         //Add savedLinearLayout to _mPagesLinearLayouts list at index _mPageIndex
         _mPagesLinearLayouts.Insert(_mPageIndex, savedLinearLayout);
    }

In creating views method:

    if(NeedToCreateViews())
    {
//create views here and add it to _mQuestionsLinearLayout
}
//Get all saved views and add it to _mQuestionsLinearLayout
else
            {
                //List of _mPagesLinearLayouts[_mPageIndex] LinearLayout views
                List<View> views = new List<View>();

                //Add all _mPagesLinearLayouts[_mPageIndex] LinearLayout to views list
                for (int i = 0; i < _mPagesLinearLayouts[_mPageIndex].ChildCount; i++)
                {
                    views.Add(_mPagesLinearLayouts[_mPageIndex].GetChildAt(i));             
                }

                //Remove all views from  _mPagesLinearLayouts[_mPageIndex] linear layout
                _mPagesLinearLayouts[_mPageIndex].RemoveAllViews();

                //Remove the linear layout at index _mPageIndex
                _mPagesLinearLayouts.RemoveAt(_mPageIndex);

                //Add all views from views list to _mQuestionsLinearLayout
                for (int i = 0; i < views.Count; i++)
                {
                    _mQuestionsLinearLayout.AddView(views[i]);
                }

            }
Hadi Al Tinawi
  • 429
  • 7
  • 22