Sorting multiple activities in Android Studio

Viewed 111

here is a more basic question, since I am not too familiar with Android Studio and I am not sure what the correct phrases are to find it in the developers guide or here on SO.

My Question:

How can I handle 3 activities (A,B,C), starting with A, going to B, and then have a constant exchange between B and C?

When using the classic Intent calling approach everything is fine from A to B but going further gets me to A not to C. There seems to be a little delay and a white screen then, like if the application tries to open activity C but fails. I am passing a seriable object through all these activities. The object gets created and initialized in A. B displays the information in the object and let the user change some of it. C let the user change a special place in my object. I other words this object works as a persistent place to store information at the exchange between B and C.

Intent call in A (MainActivity):

Intent intent = new Intent(this, GameActivity.class);
    intent.putExtra(GAME_MANAGER,gm);
    startActivity(intent);

getting the object in B(GameActivity):

gm = (GameManager) getIntent().getSerializableExtra(GAME_MANAGER);

Intent call in B (GameActivity):

 Intent intent = new Intent(this, InteractionActivity.class);
    intent.putExtra(GAME_MANAGER, gm);
    startActivity(intent);

getting the object in C(InteractionActivity):

gm = (GameManager) getIntent().getSerializableExtra("GAME_MANAGER");

Intent call in C (InteractionActivity):

Intent intent = new Intent(this, GameActivity.class);
    intent.putExtra(GAME_MANAGER,gm);
    startActivity(intent);

my Manifest:

manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.chronoplatoon">

application

    android:allowBackup="true"

    android:icon="@mipmap/ic_launcher"

    android:label="@string/app_name"

    android:roundIcon="@mipmap/ic_launcher_round"

    android:supportsRtl="true"

    android:theme="@style/AppTheme">

    activity android:name=".InteractionActivity"></activity>

    activity android:name=".GameActivity" ></activity>

    activity android:name=".MainActivity">

        intent-filter>

            action android:name="android.intent.action.MAIN" />

            category android:name="android.intent.category.LAUNCHER" />

        /intent-filter>

    /activity>

/application>

/manifest> (i got some problems displaying that. sorry for the ugly format and the deleted "<" at start of every line )

If someone could me explain how some proper handling of a complex structure of activities in Android Studio works, that would be great! If all this code is theoretically working please answer so I can work on it and find the error somewhere else. As Otid0 pointed out, the usage of the finish() method in the activity C sounds as a good approach. But then I would need some other way to let all activities work on the same object like some kind of global persistent memory. For me the activities in Android Studio seemed to be more like multiple main() functions that work independently. Please teach me better if this is false. Thanks a lot!

1 Answers

The answer of @Otid0 in the comments worked for me fine. Basically, the problem was the usage of an object through multiple activities. To solve this, the usage of a Singleton class can help. In my research I found some articles that strongly discourage the usage of Singleton classes. I assume that is because of the risks with global variables. If there are other risks or other approaches feel free to comment. Thanks a lot Otid and have a good day!

Related