简体   繁体   English

Android将非活动类的参数传递给活动

[英]Android passing arguments to activity from non-activity class

I have this problem... 我有这个问题

I have activity(CartActivity) that starts another activity(FindItemActivity). 我有启动另一个活动(FindItemActivity)的活动(CartActivity)。

In FindItemActivity there is an expandlableListView(build by custom expandalbleListAdapter) with Buttons(AddItem) with custom ActionListener(OnclickListener) 在FindItemActivity中,存在一个expandlableListView(由自定义expandalbleListAdapter生成),其中Buttons(AddItem)与自定义ActionListener(OnclickListener)

I would like to pass arguments from my actionListener to first activity(CartActivity). 我想将参数从我的actionListener传递给第一个活动(CartActivity)。

Here is what i have right now(in my on click listener): 这是我现在所拥有的(在我的点击监听器中):

public class addOnClickListener implements OnClickListener{
Product toAdd;
Context toEnd;
public addOnClickListener(Product addable, Context context){
    this.toAdd = addable;
    this.toEnd = context;
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i = new Intent(toEnd,MyCartActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
    i.putExtra("Name", this.toAdd.name);
    i.putExtra("Code", String.valueOf(this.toAdd.code));
    i.putExtra("Price", String.valueOf(this.toAdd.price));
    toEnd.startActivity(i);

}
}

But, every time the button is pressed application starts NEW CartActivity, instead of the old one, that is already running... I need data stored in original activity, so I have to resume it, not to start a new one... The compiler tells me FLAG_ACTIVITY_NEW_TASK is necessary when starting an activity from non-activity class. 但是,每次按下按钮时,应用程序都会启动NEW CartActivity,而不是已经运行的旧CartActivity。我需要将数据存储在原始活动中,因此我必须恢复它,而不是开始新的活动。从非活动类开始活动时,编译器告诉我FLAG_ACTIVITY_NEW_TASK是必需的。

Is there any other flag that can be used to resume activity instead of starting a new one? 还有其他标志可用于恢复活动而不是开始新的标志吗?

Or maybe can I close somehow activity FindItemActivity? 或者也许我可以以某种方式关闭活动FindItemActivity? I tried finish() but I was unable to find a way to use it in non-activity class... 我尝试了finish()但无法找到在非活动类中使用它的方法...

If I rightly understand, all you want is to start another activity for choosing something and return back to your first activity. 如果我正确理解,那么您想要做的就是开始另一个选择活动,然后返回到您的第一个活动。

If I am right, you must call startActivityForResult ( read more ) method in your first activity, and override onActivityResult ( read more ) method in that first activity for receiving the result from your second activity. 如果我是对的,则必须在第一个活动中调用startActivityForResult阅读更多 )方法,并在第一个活动中重写onActivityResult阅读更多 )方法以从第二个活动中接收结果。

Before you close the second activity, in which the user must take a choice, you must populate your result intent with data you want to pass back to the first activity. 在关闭用户必须选择的第二个活动之前,必须使用要传递回第一个活动的数据填充结果意图。 ( read more ) 了解更多

If you define your OnCLickListener in the same file as your FindItemActivity , you can close it using this code FindItemActivity.this.finish() from OnClickListener . 如果在与FindItemActivity相同的文件中定义OnCLickListener ,则可以使用OnClickListener这段代码FindItemActivity.this.finish()将其OnClickListener Otherwise you can pass link of you activity class to OnClickListener and invoke the finish method using this link. 否则,您可以将活动类的链接传递给OnClickListener并使用此链接调用finish方法。

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

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