在Android Studio中对多个活动进行排序。

huangapple go评论70阅读模式
英文:

Sorting multiple activities in Android Studio

问题

这是一个更基础的问题,因为我对Android Studio不太熟悉,不确定在开发者指南中或者在Stack Overflow上如何找到正确的短语。

我的问题是:

我如何处理3个活动(A、B、C),从A开始,转到B,然后在B和C之间进行恒定的交互?

当使用经典的Intent调用方法时,从A到B一切正常,但进一步进行时,我会回到A而不是C。似乎会有一点延迟和一个白屏,就像应用程序尝试打开C活动但失败了。我通过所有这些活动传递了一个可序列化的对象。该对象在A中被创建和初始化。B显示对象中的信息,并允许用户更改其中一些信息。C允许用户更改对象中的特定位置。换句话说,这个对象在B和C之间的交互中充当持久存储信息的地方。

在A(MainActivity)中的Intent调用:

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

在B(GameActivity)中获取对象:

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

在B(GameActivity)中的Intent调用:

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

在C(InteractionActivity)中获取对象:

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

在C(InteractionActivity)中的Intent调用:

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

我的清单(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>

如果有人可以解释一下在Android Studio中如何适当处理复杂的活动结构,那将会很棒!如果所有这些代码在理论上是正常工作的,请回答,这样我就可以在其他地方找到错误。正如Otid0指出的,使用活动C中的finish()方法似乎是一个不错的方法。但是那样的话,我就需要另一种方式让所有的活动都能够在同一个对象上工作,就像某种全局持久内存。对我来说,Android Studio中的活动似乎更像是多个独立运行的main()函数。如果我理解错了,请给我更好的指导。非常感谢!

英文:

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(&quot;GAME_MANAGER&quot;);

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=&quot;com.example.chronoplatoon&quot;&gt;

application

    android:allowBackup=&quot;true&quot;

    android:icon=&quot;@mipmap/ic_launcher&quot;

    android:label=&quot;@string/app_name&quot;

    android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;

    android:supportsRtl=&quot;true&quot;

    android:theme=&quot;@style/AppTheme&quot;&gt;

    activity android:name=&quot;.InteractionActivity&quot;&gt;&lt;/activity&gt;

    activity android:name=&quot;.GameActivity&quot; &gt;&lt;/activity&gt;

    activity android:name=&quot;.MainActivity&quot;&gt;

        intent-filter&gt;

            action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;

        /intent-filter&gt;

    /activity&gt;

/application&gt;

/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

得分: 0

@Otid0 在评论中的回答对我非常有用。
基本上,问题出在通过多个活动(activities)中使用一个对象。
为了解决这个问题,**使用单例模式(Singleton class)**可以帮助解决。在我的研究中,我找到了一些强烈不建议使用单例模式的文章。我认为这可能是因为全局变量带来的风险。如果还有其他风险或其他方法,请随意评论。非常感谢 Otid,祝你有美好的一天!

英文:

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!

huangapple
  • 本文由 发表于 2020年8月15日 23:44:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63427909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定