gpt4 book ai didi

android - 如何在 fragment 中实现 onBackPressed() 和 Intent ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:25 25 4
gpt4 key购买 nike

我知道 onBackPressed() 是 Activity 中的一种方法,但我想在 fragment 中使用该功能,这样当按下后退按钮时,它会通过 Intent 重定向到另一个 Activity 。有什么解决办法吗?

public class News_Events_fragment extends Fragment {
ProgressDialog pd;
ListView lv1;
SharedPreferences sharedPreferences = null;
int NotiCount;
TextView txt_title, txt_msg, textView;
Context context;
Intent intent ;
ArrayList<SliderMsgTitleModel> CurrentOfficersPastList;
NewsActivityAdapter pastAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

context = (Context) getActivity();

View rootView = inflater.inflate(R.layout.activity_news, container, false);

new AsyncTask<Void, Void, ArrayList<SliderMsgTitleModel>>() {

protected void onPreExecute() {
pd = new ProgressDialog(getActivity());
pd.setCancelable(true);
pd.setTitle("UPOA");
pd.setMessage("Please wait,loading the data...");
pd.show();
}

@Override
protected ArrayList<SliderMsgTitleModel> doInBackground(
Void... params) {
System.out.println("In Background");
CurrentOfficersPastList = new ArrayList<SliderMsgTitleModel>();
// display view for selected nav drawer item
ParseQuery<ParseObject> query = ParseQuery.getQuery("message");
query.whereEqualTo("featured_status", true);
// query.whereEqualTo("push_status", true);

query.orderByDescending("updatedAt");

query.selectKeys(Arrays.asList("title"));
query.selectKeys(Arrays.asList("message"));
try {
query.setCachePolicy(ParseQuery.CachePolicy.NETWORK_ELSE_CACHE);
List<ParseObject> results = query.find();
for (int i = 0; i < results.size(); i++) {
ParseObject object = results.get(i);
CurrentOfficersPastList.add(new SliderMsgTitleModel(
object.getString("title"), object
.getString("message")));
System.out.println("title is=="
+ object.getString("title") + "&& message is"
+ object.getString("message") + "size is"
+ CurrentOfficersPastList.size());

}
} catch (Exception e) {
e.getMessage();
}
pd.dismiss();

return CurrentOfficersPastList;
}

@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(ArrayList<SliderMsgTitleModel> value) {

pd.dismiss();
/*Intent ent = new Intent(getActivity(), NewsActivity.class);
ent.putExtra("NEWSLIST", (ArrayList<SliderMsgTitleModel>) value);
startActivity(ent);
System.out.println("Value is" + value.size());*/

CurrentOfficersPastList = new ArrayList<SliderMsgTitleModel>();
CurrentOfficersPastList = value;
lv1 = (ListView) getActivity().findViewById(R.id.list_title);
pastAdapter = new NewsActivityAdapter(getActivity(), R.layout.activity_news_txt, CurrentOfficersPastList);
lv1.setAdapter(pastAdapter);

}
}.execute();

return rootView;
}

public void onBackPressed() {
// TODO Auto-generated method stub
//super.onBackPressed();
//Toast.makeText(getApplicationContext(), "click",2000).show();
String cameback="CameBack";
intent = new Intent(getActivity(),HomeActivity.class);
intent.putExtra("Comingback", cameback);
startActivity(intent);
}

}

最佳答案

您可以使用回调接口(interface)与 fragment 交互。在您的 Activity 中添加以下内容:

public class MyActivity extends Activity {

protected OnBackPressedListener onBackPressedListener;

public interface OnBackPressedListener {
void doBack();
}

public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
this.onBackPressedListener = onBackPressedListener;
}

@Override
public void onBackPressed() {
if (onBackPressedListener != null)
onBackPressedListener.doBack();
else
super.onBackPressed();
}

@Override
protected void onDestroy() {
onBackPressedListener = null;
super.onDestroy();
}
}

在您的 fragment 中添加以下内容:

public class MyFragment extends Fragment implements MyActivity.OnBackPressedListener {

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((MyActivity) getActivity()).setOnBackPressedListener(this);
}

@Override
public void doBack() {
//BackPressed in activity will call this;
}

}

关于android - 如何在 fragment 中实现 onBackPressed() 和 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22552653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com