'Restrict an activities SetContentView to display inside an existing container

I have a complex imported package that creates an activity and then uses SetContentView on an R.layout file. This layout essentially inflates and covers the screen, but I would like it to only "inflate" inside an existing view that is attached to MainActivity.

The activity class looks something like this

public class ExampleActivity extends AppCompatActivity {
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    setTheme(R.style.Theme_AppCompat_NoActionBar);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example_layout);
  }
}

And I would call it from something like a clickListener with a function like this (where activity is MainActivity)

public static void startNewActivity(Activity activity) {
    Intent navigationActivity = new Intent(activity, ExampleActivity.class);
    activity.startActivity(navigationActivity);
  }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AFAFB1"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="viewToFill">
    </LinearLayout>
     

</androidx.coordinatorlayout.widget.CoordinatorLayout>

So the question I'm asking is would it be possible to set an activity up so that when SetContentView is called a predefined view (attached to MainActivity) is filled instead of the entire screen. In simple terms, have SetContentView put R.layout.example_layout into the linearlayout viewToFill in activity_main.xml.

Any help would be appreciated, I have ideas of working around it, it's just that this solution is by far the least involved if it's possible, but I'm very aware it may not be

edit: made it clearer that the target layout is attached to a different activity than the new layout created by SetContentView



Solution 1:[1]

As per the official documentation in Android application components the Activity covers the entire screen / window which is the correct and desired behaviour.

If you want to start a view that only covers the partial screen then you should use a Fragment. A Fragment represents a reusable portion of your app's UI and provides the modularity that you want.

Solution 2:[2]

Just define Theme.AppCompat.NoActionBar for that Activity in AndroidManifest.xml.

Solution 3:[3]

The essence of this question was "can I have 2 activities running on the same screen" and the answer to that question is no, it is not currently possible (for the most part).

I say for the most part because I actually stumbled across exactly what I was looking for at https://developer.android.com/guide/topics/large-screens/activity-embedding, activity embedding on a phone screen is what I was looking for which is the ability to run 2 activities with ui elements on the same screen. However as the docs say this is an experimental API that is only for newer large screen devices, so it is still very early days for this technology and there's a good chance that it never comes to small screen devices.

I came across fragments a lot both here and researching this question, but a fragments intention is to give a single activity reusable portions of the UI and to provide lifecycles to those portions, among a host of other features.

So with the complex package I have, I am going to find what the activity lifecycle provides to it, and find another way of mimicking it without it having to start a new activity and then I'll replace the setcontentview with a regular inflation pointed somewhere in activity_main.xml

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Taranmeet Singh
Solution 2 Martin Zeitler
Solution 3 Jwit