1

Newbie here. I'm still trying to understand the concept of running a single process or a purpose within multiple layouts.

For example, I want a "post submission" feature on my application. There's a main layout that contains textboxes of title, content, etc. and a button that links to another layout for picking category. My question is, how can I integrate those two layouts/activities? How can I pass the picked category to the main layout?

I just thought these ways:

  1. Using static variable (?)
  2. Using fragment

Can someone explain the correct way? So sorry if this's a really basic question.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sena
  • 178
  • 1
  • 13
  • 1
    You have many options: [IPC mechanisms availiable in Android](https://stackoverflow.com/questions/5740324/what-are-the-ipc-mechanisms-available-in-the-android-os), or a database/shared preferences, or remote storage (server, Firebase), or an EventBus library...there are a lot! – PPartisan Sep 19 '18 at 13:40
  • @PPartisan thanks, I think I will take a look for Intent. – Sena Sep 19 '18 at 13:45

1 Answers1

1

If I am understanding this correctly you are just switching between two simple layouts and passing information back and forth.

I would recommend reading up on how Fragments work. https://developer.android.com/guide/components/fragments

To give a brief overview, I would use one main Activity (the parent activity) and then use two fragments to handle the user interactions.

Fragment 1: Textbox, Content, Button Fragment 2: Categories

Passing information between fragments is pretty straight forward as well https://developer.android.com/training/basics/fragments/communicating

ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);

You can pass information using bundles. I recommend playing around with Fragments and Activities to become a little more comfortable.

  • Is it possible to use a single FrameLayout for multiple fragments, or should I add FrameLayout for each fragment, and show/hide the FrameLayout? – Sena Sep 19 '18 at 14:04
  • 1
    You can use a single Frame Layout for multiple. Your main activity will inflate your FrameLayout. Each fragment will exist inside of that frame layout. Then each fragment inflates their own layout as well. There should be a total of 3 layouts. One for the activity (Frame Layout) and one for each fragment – Zachery Misson Sep 19 '18 at 14:43