【发布时间】:2018-02-28 21:00:12
【问题描述】:
这让我很烦恼。
我想在不影响片段可重用性的情况下从片段中获取活动。这意味着我不能直接从片段内部启动一个 Intent 到该片段当前附加到的活动。
这是有问题的代码
//This is the code inside the fragment
public void onStart() {
super.onStart();
View view = getView();
switch (position){
//case 1 is the case when the user clicks the ADD List Item from a ListFragment.
//Position is the position of ADD in the List Fragment.
case 1:{
Intent intent = new Intent(/*what exactly should I put here?*/ ,
/*this is where the reference to activity the fragment is attached to goes.
But we dont know what activty the fragment is attached to, as it is reusable
and may get attached to different activity at different times*/);
}
case 2:{
//this is the case when user decides to view the entered text in the array list.
TextView textView = (TextView)view.findViewById(R.id.display_name);
int size = workout.arrayList.size();
Object[] array = workout.arrayList.toArray();
for(int i=0;i<array.length;i++){
textView.setText((String)array[i] + "\n");
}
}
}
}
我觉得数据可能不足,虽然我不确定还能提供什么,对此感到抱歉。
如果需要更多数据,请告诉我。
【问题讨论】:
-
你到底想要什么??干扰可重用性是什么意思..
-
片段以其可重用性而闻名,为了做到这一点,片段和活动不应直接相互通信。您通过接口进行通信。由于片段可能与其他活动一起使用,因此这是必不可少的
-
仍然没有得到你想要达到的目标??
-
一种访问片段所附加到的活动的方法。
-
参加活动是什么意思?打开父活动??从片段本身??
标签: java android android-activity fragment