2016-09-27 58 views
0

想象这种堆栈情况: A - B- C - D - B,A,B和C是活动,但D是服务。重复使用相同的活动

基本上,我有一个服务(D),并从那个服务我想调用一个已经存在的活动(B)。我知道如果我想重新使用一个活动,我所要做的就是使用标志(或者更改清单)SingleTop(如果它已经在顶部将重新使用该活动)或SingleTask (将重新使用该活动是否处于最佳状态)。

问题是,因为我在一个服务中,我将不得不添加标志FLAG_ACTIVITY_NEW_TASK,以便我可以调用一个活动。另外,我在我的清单中添加了SingleTask作为启动模式,以便该活动将被重新使用。

这很好,因为它重新使用相同的活动并返回到onNewIntent(意图意图)方法。 问题是,我把所有的东西都作为一个额外的意图来作为null。我尝试通过该意图发送2个字符串和2个布尔值,并且它们都以空值到达onNewIntent(意图意图)。

我该如何解决这个问题?在获得额外资讯之前,我必须在onNewIntent(Intent intent)方法中做些什么吗?有没有更好的选择?

PS: 我听说过StartActivityForResult或类似的东西。这只能工作50%的时间,因为这是一个“类似聊天”的应用程序。

所以我会在“聊天”中,从我去哪里去另一个活动,在那里我可以选择要发送的东西。在那之后,我会去完成转移的服务,然后回到“聊天”。 但是当我收到一些东西时,我已经在“聊天”中了,所以startActivityForResult在这种情况下不起作用(要接收的服务将在后台运行+我不想完成接收部分,因为我想总是在听某些东西)。

下面是该服务的代码,我尝试重新启动单一活动:

 Intent transfRec=new Intent(ServerComm.this ,TransferRecordActivity.class); 
          transfRec.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

          transfRec.putExtra("receivedFilePath",appName+".apk"); 
          transfRec.putExtra("joinMode","appselection"); 
          transfRec.putExtra("nameOfTheApp",appName); 
          transfRec.putExtra("received",false); 

          transfRec.putExtra("isHotspot",isHotspot); 
          startActivity(transfRec); 

这里是我的onNewIntent的代码(意向意图):

protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    System.out.println("I am here in the new intent"); 
    if(intent==null){ 
     System.out.println("Intent is null inside the new intent method !!!"); 
    } 
    tmpFilePath=getIntent().getStringExtra("receivedFilePath"); 
    System.out.println("The tmpFilePath is : "+tmpFilePath); 
    received=getIntent().getBooleanExtra("received",true); 
    nameOfTheApp=getIntent().getStringExtra("nameOfTheApp"); 
    isHotspot=getIntent().getStringExtra("isHotspot"); 
    System.out.println("O received boolean esta a : : : "+received); 
    textView.setVisibility(View.GONE); 

    receivedFilePath= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+tmpFilePath; 
    System.out.println("transfer REcord Activity _: the received file path is :::: " +receivedFilePath); 

    getReceivedApps(); 

    adapter= new HighwayTransferRecordCustomAdapter(this,listOfItems); 
    receivedAppListView.setAdapter(adapter); 

编辑:正如你们可以看到我检查意图是否为空,而事实并非如此,因为它没有执行system.out.println这个条件!

回答

1

问题是您在onNewIntent()内拨打getIntent()。来自getIntent()的文档:

返回开始此活动的意图。

因此,你得到提供给onCreate()intent。为了获得提供给onNewIntent()intent,您只需使用intent这是在方法签名提供:

protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    tmpFilePath=intent.getStringExtra("receivedFilePath"); 
    ... 
} 
+0

谢谢哥哥。现在我解决了这个问题,但是这些东西仍然没有加载。它们不是null,但是listview不会“刷新”或者不显示它显示的项目。你认为我应该问另一个问题,或者只是改变这个问题? –

+0

是的,请这样做,因为这个问题与您的原始查询无关。 – Shaishav

+0

是的,谢谢你,兄弟,我会做到的。 –

相关问题