Index

Android apps - Multiple Screens

See AndroidStudio's tutorial on Intent.

Define a second screen
Moving to from MainActivity to second screen
Accessing data across screens
Accessing the context

Define a second screen

In app/manifests/AndroidManifest.xml, there is already an 'activity' for 'MainActivity', as follows:

<activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

Add another for your second screen, called (in this case) 'Screen2'. (This should go before '<'MainActivity'/application>'.)

<activity android:name=".Screen2" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.edkins.mazes.MainActivity" /> </activity>

Now add a second screen layout. Right click on app/res/layout, then 'New', then 'Layout Resource file'. Specify a name of (for example) 'screen2layout'. You can copy stuff across from app/res/layout/activity_main if you want, altering the parts of the layout that you want to change. There is one thing that you must change, the "tools:context" in the bit at the top.

tools:context=".Screen2"

You also need to add the code to process the screen (in Java). Right click app/java/com.(the project name)/MainActivity, then 'New', then Java Class. Specify a name of 'Screen2' (this name is the one in the manifest).

In this new class ('Screen2'), here is the code after the packagae and imports:

public class Screen2 extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen2layout); // anything you need to do when entering the screen } // any method needed, e.g. to process buttons, input, output, etc. }

Moving to from MainActivity to second screen

Set up the necessary layout in app/res/layout/activity_main to action the move, perhaps a button, with an onClick of 'go2' (or whatever you want).

In app/java/com.(the project name)/MainActivity:

public void go2(View view) { Intent intent = new Intent(this, Screen2.class); startActivity(intent); }

Note the name 'Screen2' in the intent statement.

So when you run the app, 'MainActivity' gets run. When you click on a button in 'MainActivity', 'Screen2' gets run. This will have an arrow in the title bar which means that the user can get back to 'MainActivity'.


Accessing data across screens

The easiest way is probably to define the fields you want in app/java/com.(the project name)/MainActivity as 'public static', as global variables, before the 'onCreate' method but after the Class defintion, e.g.

public static String message;

This can then be accessed in 'MainActivity' as 'message', and in 'Screen2' as 'MainActivity.message'. Either can alter the field as well as reading it.


Accessing the application context

In the app/java/com.(the project name)/MainActivity, you sometimes use 'MainActivity.this', e.g. in Toast. This won't work in 'Screen2'. Use 'getApplicationContext()' instead.



© Jo Edkins 2016 - Index to Android app index