简体   繁体   English

活动重用逻辑

[英]Activity re-use logic

I have an activity B that is generated from an activity A. In the activity B, i have a button that will send me to the activity C. Inside the Activity C i will run a service and inside that service i want to go back to activity B. 我有一个从活动A生成的活动B。在活动B中,我有一个按钮会将我发送到活动C。在活动C中,我将运行一个服务,在该服务中,我想返回到活动B。

So it would be A->B->C -> service->B. 因此它将是A-> B-> C->服务-> B。

That B activity is like a chat/history of apk transfer and right now i am still not using a database (later i will change the code for that, i can't right now). B活动就像是apk传输的聊天记录/历史记录,现在我仍未使用数据库(稍后我将为此更改代码,我现在不能)。

The data that will be shown in Activity B, comes from that service ( i will send apk files) and after the files were sent i want to retrieve to the activity B the name of the app that i sent, so that i can display it on that chat/history. 活动B中将显示的数据来自该服务(我将发送apk文件),并且在发送文件后,我想向活动B检索我发送的应用程序的名称,以便我可以显示它在那个聊天/历史上。

If inside the service i create a new intent that will run the activity B, it will always start the activity B again, instead of "keeping" the old information that was already there. 如果在服务内部,我创建了一个将运行活动B的新意图,它将始终再次启动活动B,而不是“保留”已经存在的旧信息。

How do you guys think i should do this ? 你们怎么认为我应该这样做?

I know that there is a flag that will call the activity B that is already in the stack (and that when that happens it goes to the onResume() method (right?) ), but are there any alternatives ? 我知道有一个标记将调用堆栈中已经存在的活动B(并且在发生这种情况时,它将转到onResume()方法(对吗?) ),但是还有其他选择吗?

What is the best way to do this ? 做这个的最好方式是什么 ?

Thank you guys ! 感谢大伙们 ! Sorry if it was too confusing. 抱歉,太混乱了。

To make ActivityB remain, place following in manifest : 要保留ActivityB,请在manifest中放置以下内容:

<activity android:name="ActivityB"
   android:launchMode="singleTask" />

This will keep activity. 这将保持活动。 And onCreate will be called only once. 并且onCreate将仅被调用一次。 All further request should be handled in onNewIntent() . 所有其他请求应在onNewIntent()处理。

From LocalActivityManager.java startActivity doc: LocalActivityManager.java startActivity文档:

if the current activity uses a non-multiple launch mode (such as singleTop ), or the Intent has the FLAG_ACTIVITY_SINGLE_TOP flag set, then the current activity will remain running and its Activity.onNewIntent() method called. 如果当前活动使用非多次启动模式(例如singleTop ),或者Intent设置了FLAG_ACTIVITY_SINGLE_TOP标志,则当前活动将保持运行状态,并调用其Activity.onNewIntent()方法。

From your FirstActivity call the SecondActivity using startActivityForResult() method 从您的FirstActivity中,使用startActivityForResult()方法调用SecondActivity

For example: 例如:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

In your SecondActivity set the data which you want to return back to FirstActivity. 在您的SecondActivity中,设置要返回到FirstActivity的数据。 If you don't want to return back, don't set any. 如果您不想返回,请不要进行任何设置。

For example: In secondActivity if you want to send back data: 例如:如果要发送回数据,请在secondActivity中:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();

If you don't want to return data: 如果您不想返回数据:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();

Now in your FirstActivity class write following code for the onActivityResult() method. 现在,在FirstActivity类中,为onActivityResult()方法编写以下代码。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}//onActivityResult

Start Activity C by using 通过使用启动活动C

Intent nextIntent = new Intent(this, ActivityC.class);
startActivityForResult(nextIntent, requestCode);

On service you can finish the activity with result code to identify whether it is success or failure. 在服务中,您可以使用结果代码来完成活动,以识别活动是成功还是失败。 In Activity B receive the result by onActivityResult. 在活动B中,通过onActivityResult接收结果。 If it is success please pull the data from database(use singleton Datahelper class to set/get data as you did not implemented database now. Use Realm which is very easy to integrate and handle). 如果成功,请从数据库中提取数据(使用单例Datahelper类来设置/获取数据,因为您现在尚未实现数据库。请使用Realm,它非常易于集成和处理)。

I guess this will solve your problem. 我想这会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM