Android fragments 添加片段时多次调用Android annotations@Afterview
我有一个片段,每当它被添加到布局@afterviews时,都会被调用 例如,在父活动中,我检查片段是否已创建,如果未创建,则将其膨胀。然后我检查片段是否已添加到布局中,如果未添加,则添加它并显示新片段Android fragments 添加片段时多次调用Android annotations@Afterview,android-fragments,android-annotations,Android Fragments,Android Annotations,我有一个片段,每当它被添加到布局@afterviews时,都会被调用 例如,在父活动中,我检查片段是否已创建,如果未创建,则将其膨胀。然后我检查片段是否已添加到布局中,如果未添加,则添加它并显示新片段 @Click(R.id.tvActionFriends) void friendsClicked() { FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); if (frie
@Click(R.id.tvActionFriends)
void friendsClicked() {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (friendsFragment == null) {
friendsFragment = new FriendsFragment_();
}
if (!friendsFragment.isAdded()) {
app.log("not added adding fragment");
fragmentTransaction.add(R.id.layoutMainContainer, friendsFragment);
}
fragmentTransaction.show(friendsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
第一次单击它时,它会正常地经历生命周期
现在用户按下了后退按钮,我可以看到在Pause()onDestroy()和onDetach()上的拆卸
因此,现在friendsFragment已从布局中删除,但该类仍然存在
现在,用户再次单击按钮,在主机活动中显示friendsFragment。这一次,片段已经存在,但不在布局中,因此将重新添加它并提交事务
现在,片段经历了创建的生命周期,但是@afterview方法会被触发两次,如果用户再次点击并第三次单击按钮,@afterview将被触发三次等等
这是朋友节
@EFragment(R.layout.fragment_friends)
public class FriendsFragment extends BaseFragment {
ArrayList<String> numbers, facebookIds, emails;
ArrayList<Friend> friends;
@Bean
FriendsAdapter friendsAdapter;
@ViewById(R.id.lvFriends)
CustomListView lvFriends;
boolean gettingFriends = false;
@AfterInject
void initInject() {
super.baseInject();
numbers = new ArrayList<String>();
facebookIds = new ArrayList<String>();
friends = new ArrayList<Friend>();
friendsAdapter.setItems(friends);
}
@AfterViews
void initViews() {
super.baseViews();
lvFriends.setAdapter(friendsAdapter);
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
if (phoneNumber != null) {
phoneNumber = phoneNumber.replace("+", "");
numbers.add(phoneNumber);
}
}
phones.close();
getFriends();
}
OnFriendsListener onFriendsListener = new OnFriendsListener() {
@Override
public void onComplete(List<Profile> friends) {
for (Profile p : friends) {
app.log(p.getName());
}
}
@Override
public void onException(Throwable throwable) {
app.log(throwable.getMessage());
};
@Override
public void onFail(String reason) {
app.log(reason);
}
};
@Background
void getFriends() {
if (gettingFriends == true)
return;
gettingFriends = true;
friends.clear();
MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();
data.add("numbers", numbers.toString());
data.add("facebook_ids", facebookIds.toString());
ArrayList<Friend> resp = snapClient.recommendFriends(data);
friendsCallback(resp);
}
@UiThread
void friendsCallback(ArrayList<Friend> resp) {
if (resp != null)
friends.addAll(resp);
friendsAdapter.notifyDataSetChanged();
gettingFriends = false;
}
}
我不知道这是安卓系统的注释问题,我搞砸了,还是有碎片,我们非常感谢您的帮助。看起来这可能会在AndroidAnnotations 3.3中修复
根据上的一个问题,在上有一个拉取请求。我记不清了,但我想我们以前也有过类似的问题。你能在GitHub中搜索问题吗?不是ActiveAndroid,而是AndroidAnnotations。但是,是的,这个问题应该得到解决。
@EFragment
public class BaseFragment extends Fragment {
@App
MyApp app;
@SystemService
LayoutInflater layoutInflater;
@Bean
VideoCache videoCache;
@RestService
CloudClient cloudClient;
@Bean
MyResources myResources;
@ViewById(R.id.tvActionExtra)
TextView tvActionExtra;
SnapClient snapClient;
FragmentManager fragmentManager;
SimpleFacebook simpleFacebook;
void baseInject() {
app.log("init inject " + this.getClass().getCanonicalName());
app.currentContext = getActivity();
snapClient = app.snapClient;
}
void baseViews() {
app.log("init views " + this.getClass().getCanonicalName());
}
@Override
public void onAttach(Activity activity) {
Log.d(MyApp.TAG,"attach " + this.getClass().getCanonicalName());
super.onAttach(activity);
if (activity instanceof MainActivity) {
try {
MainActivity a = (MainActivity) activity;
simpleFacebook = a.simpleFacebook;
}
catch (Exception e) {
e.printStackTrace();
}
}
}
void toast(String message) {
app.toast(message);
}
AlertDialog simpleAlert(String title, String message) {
return MyApp.simpleAlert(getActivity(), title, message);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(MyApp.TAG,"create " + this.getClass().getCanonicalName());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(MyApp.TAG,"create view " + this.getClass().getCanonicalName());
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onPause() {
Log.d(MyApp.TAG,"pause" + this.getClass().getCanonicalName());;
super.onPause();
}
@Override
public void onResume() {
Log.d(MyApp.TAG,"resume " + this.getClass().getCanonicalName());
super.onResume();
}
@Override
public void onDetach() {
Log.d(MyApp.TAG,"detach " + this.getClass().getCanonicalName());
super.onDetach();
}
@Override
public void onDestroy() {
Log.d(MyApp.TAG,"destroy " + this.getClass().getCanonicalName());
super.onDestroy();
}