0

So full admission, I am a bit self taught when it comes to Android Dev, so I maybe going about this all wrong. As such I am open to suggestions! I'm essentially trying to semi-automate a task I do every day currently.

Question: How to Pass a LinearLayout, it's contents intact, between Activities?

So I have this 2nd Activity, called reportGeneratorActivity In this activity there is a Linear Layout directly under the Report Preview.

The Linear Layout itself is defined in a separate XML file as previewplate.xml

Now this Activity functions, when you put text into the upper fields it updates the preview below. Which brings me to the brick wall I'm hitting. The Goal is to take that preview plate and add it to my main activity that I've named rootActivity in the white area which is a Linear Layout itself named rootWorkingLayout.

Now the Strings from the text are all stored temporarily in reportGeneratorActivity at which point I am doing this when the button is pressed:

 public void beginReport (View view) {
    //Bundle the Preview
    Bundle previewBundle = new Bundle();//Create the Bundle
    previewBundle.putString("date", dateHolder);
    previewBundle.putString("client", clientNameHolder);
    previewBundle.putString("machine", machineTypeHolder);
    previewBundle.putString("serial#", serialNumberHolder);
    previewBundle.putString("notes", notesHolder);
    // Prepare The Intent
    Intent previewPasser = new Intent(this, rootActivity.class);
    previewPasser.putExtras(previewBundle); // Add the Bundle to Intent
    //Send Preview to Root
    startActivity(previewPasser);
    //Send Preview to History
    //Send User to Decision Tree
    }

From what I understand, I've put all the strings in Bundle previewBundle, then attached the bundle to the previewPasser intent and sent the intent back to rootActivity.

In rootActivity, within the onCreate function I have placed this code:

    Bundle previewReceiver = getIntent().getExtras();
    //If There is a Bundle, Process it
    if(previewReceiver != null) {
        newPreview(previewReceiver);
    }

The Goal here is to grab the Intent, and grab the bundle then pass it to my newPreview function (currently empty) that will duplicate finished preview from report_generator_activity and desplaying within the Linear Layout: rootWorkinglayout in an identical fashion.

It's this final step that I am hitting a brick wall on, I can only assume there is an easier way, perhaps a way to duplicate the Layout and it's contents and send it over? Or if I am doing this functionally, How do I unpack the data in an identical manner?

Please forgive the verbosity and lack of images as I am a new member of the community.

Edit #1: In response to SoroushA's excellent answer that has put me on the correct path, I've adjusted my newPreview Method to be this:

public void newPreview (Bundle previewReceiver) {
    //Extract Strings from Bundle
    String date = previewReceiver.getString("date");
    String client = previewReceiver.getString("client");
    String machine =previewReceiver.getString("machine");
    String serialNum = previewReceiver.getString("serial#");
    //Create New Inflater
    LayoutInflater previewInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View previewLayout = previewInflater.inflate(R.layout.previewplate, null);
    //Add previewLayout to rootWorkingLayout
    rootWorkingLayout.addView(previewLayout);
}

Currently, I am just trying to get the grey box of the preview plate layout to appear as it's background is defined in it's own XML file. However, nothing is occurring when I go through the process clearly due to my own error. I am unsure of what step I am missing.

Thanks in advance!

VanDroid
  • 29
  • 8
  • You should send the values for each container. For make it easy create a custom view for using the same view both activities. Also you shouldn't a mount of data into the bundle. Some phones does not have a lot of memory for this – Abdullah Tellioglu May 03 '16 at 18:35

2 Answers2

0

How to Pass a LinearLayout, it's contents intact, between Activities?

You don't. Widgets and containers (i.e., subclasses of View) are owned by their activity.

I can only assume there is an easier way

Have only one activity, altering its UI as needed (e.g., use fragments and replace them as needed). These seem to be way too closely coupled to be two separate activities.

The Goal here is to grab the Intent, and grab the bundle then pass it to my newPreview function (currently empty) that will duplicate finished preview from report_generator_activity and desplaying within the Linear Layout: rootWorkinglayout in an identical fashion.

There is nothing intrinsically wrong with this approach. You act as though you are having problems implementing it ("How do I unpack the data in an identical manner"), but we do not have nearly enough information on which to provide you with much advice. In general, a Bundle has getter methods, to retrieve that values that you put into the Bundle via the setter methods.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

I hope that I understood your question clearly.

In your newPreview method, start by getting the Strings back from the Bundle:

public void newPreview(Bundle preview){
String dateHolder = preview.getString("date"); 
//similarly for other strings
}

Then inflate the LinearLayout and start setting its elements.

LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_xml, null);
//call findviewbyid on the layout and set its children using the strings you extracted
SoroushA
  • 2,043
  • 1
  • 13
  • 29
  • Thank you SoroushA, I think I understand the direction here. I think however one of the gaps in my self training has left me missing an element of your answer. I can confirm that the bundle is passing the strings, but the layout is still not being created within rootWorkingLayout. Would you mind expanding on the second half of your answer? Thanks! – VanDroid May 03 '16 at 18:45
  • any layout has to be inflated before you can use it. Usually, activities inflate a layout using setContentView(activity_layout). However, if you created a new activity, and gave it a layout using setContentView and then try to open another xml file and work with that, you have to inflate it manually first using a LayoutInflater. In my answer after inflating your_xml and getting View layout, you can call layout.findViewById(R.id.some_child) to get the TextViews and other Views within that. see [here](http://stackoverflow.com/questions/3477422/what-does-layoutinflater-in-android-do?rq=1) – SoroushA May 03 '16 at 18:51
  • I think I understand, but to make sure I am following I will add a comment above in question where I can add code. – VanDroid May 03 '16 at 19:01
  • looks good. don't forget to use the strings to setText on previewLayout's elements before adding it it rootWorkingLayout. Also, consider accepting the answer if it helped you. – SoroushA May 03 '16 at 19:08
  • It did help! I'm just still unable to get previewLayout to appear on the rootWorkingLayout. Am I missing a step? – VanDroid May 03 '16 at 19:10
  • did you set its views like i said? for example to get a TextView, call myTextView = previewLayout.findViewById(R.id.the_textview) and then myTextView.setText(a_string). Also adding a layout to another parent layout depends on the layout parameters of both layouts. see [this one](http://stackoverflow.com/a/6216705/5935255) – SoroushA May 03 '16 at 19:18
  • I'm not sure I understand what you mean by setting it's views. I've yet to begin adding the Text Views to it as I can't even get the basic grey of the empty layout to appear. I've previously attempted to just add a basic button but nothing I seem to add dynamically shows up in the layout. – VanDroid May 03 '16 at 19:50
  • do you set its LayoutParams to wrap content? like [this](http://stackoverflow.com/a/11971553/5935255)? try that. – SoroushA May 03 '16 at 19:58