-1

Files:

  • activity_main.xml (button1,button2)

  • layout1.xml (button3,button4,button5)

  • layout2.xml (button6,button7,button8)

  • layoutA.xml (multiple CheckBox and textView)

  • layoutB.xml (multiple CheckBox and textView)

  • layoutC.xml (multiple CheckBox and textView)

  • layoutD.xml (multiple CheckBox and textView)

  • layoutE.xml (multiple CheckBox and textView)

  • layoutF.xml (multiple CheckBox and textView)

Okay so what I want my app to do is:

Step 1- First Screen activity_main.xml with 2 buttons

Step 2- User clicks button1/button2 and layout1.xml/layout2.xml shows up.

Step 3- User clicks any of the 3 buttons in any of the 2 layouts and accordingly layoutA/B/C/D/E/F with checkboxes and textview to show up.

Step 4- User clicks Back button and instead of completely exiting the app, roll back only to the previous active layout like layoutA to layout1, layout1 to activity_main,etc.

I have been able to implement the first 2 steps but unable to do the other half.

I would be glad if someone could help me out. Thank You!

This is my MainActivity.java file

public class MainActivity extends Activity
{

    Button button1;
    Button button2;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                setContentView(layout1);
            }
        });
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                setContentView(R.layout.layout2);
            }
        });
    }
}
Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26

1 Answers1

2

Start new Activity

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);

Start Activity with parameters

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("firstKeyName","FirstValue");
myIntent.putExtra("secondKeyName","SecondValue");
startActivity(myIntent);

Read data in started activity

Intent myIntent = getIntent(); // get previously intent
String firstKeyName = myIntent.getStringExtra("firstKeyName"); // this return "FirstValue"
String secondKeyName= myIntent.getStringExtra("secondKeyName");
Israel
  • 436
  • 3
  • 12
  • Thank You so much. It did work. So i am opening activity 1, button click takes me to act2, button click takes me to act3. When i press the back button, it takes me from act3 to act1. Any wat to go back to act 2 instead of act1? I have about 30 layout files for act3 and it would be difficult for me to create a seperate class for each of them. Any simplier method? Thanks! – Gourav Bhattacharjee Apr 22 '17 at 12:13
  • Check my edit, you can add values to the intent, in the next activity get them and load a layout or another according to the data sent using only one class. "if(firstKeyName.equals("open 1")){ load layout X } else if(firstKeyName.equals("open 2")){ load layout Y }... About to return to the previous activity, if activity 1 opens activity 2 and 2 opens 3, pressing the back button will return in the same order, check that you do not call the finish() method this destroys the activity, and dont overide onBackPressed() method – Israel Apr 22 '17 at 14:37
  • Thanks for the reply but I am very new to android development so can you help me out by posting the code with the file names I have provided above? – Gourav Bhattacharjee Apr 22 '17 at 18:06
  • i mean this: https://pastebin.com/g5g8arsc instead, you can use fragment activity and inflate one fragment class. You have many ways to do it. In my example, MainActivity open SecondActivity this show layout1 or 2 according to the parameter passed, for layoutA, B, C.... you can create a third class, open it by passing the parameter and inflate the layout that corresponds, so with 3 classes you will have encapsulated all layouts – Israel Apr 23 '17 at 07:32
  • As I can see, they have made separate classes for layout A/B/C. Is there any way I can do it with activity_main, layout1(A/B/C) and layout2(D/E/F) only? – Gourav Bhattacharjee Apr 24 '17 at 03:41
  • https://pastebin.com/uiSeFkDV You may replace ng737 with layout1 You can check my code here The only problem I am facing is that when I press back button, it takes me from layoutA to activity_main, while I want it to take me to layout1. – Gourav Bhattacharjee Apr 24 '17 at 03:46
  • https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle read this. When you press the back button, you are closing that activity, if you change the view in the same activity, pressing back, there is no "activity layoutA" you continue in layout1. To return, you need to create a new class and open it with intent. Instead, you can overwrite the back button to display a layout acording to the current: https://pastebin.com/M0QDnCKD About the classes, in my example has the same class for layoutA, B, C..., the same for layout1 and 2 and a third for main activity – Israel Apr 24 '17 at 06:02
  • https://pastebin.com/d7WxDN3K This a part of code for layout 2 where ActivityThree contains https://pastebin.com/uYYhpnPr Any way to solve this out? – Gourav Bhattacharjee Apr 24 '17 at 07:28
  • Also pastebin.com/M0QDnCKD uses R.id.layoutA which is not valid for a layout. Could you help me resolve that as well? – Gourav Bhattacharjee Apr 24 '17 at 07:49
  • can u send me a email ? So this is not a chat xd kingcreekyseniita@gmail.com – Israel Apr 24 '17 at 09:30
  • Please check email. – Gourav Bhattacharjee Apr 26 '17 at 09:19
  • I did not receive anything – Israel Apr 26 '17 at 11:31