Android的后退按钮与Fragment和他的活动不同的动作
Posted
技术标签:
【中文标题】Android的后退按钮与Fragment和他的活动不同的动作【英文标题】:Android Back button not the same action from Fragment and his activity 【发布时间】:2015-06-27 21:18:13 【问题描述】:我有这个:
MainActivity.java:
package activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import com.example.pierre.tan.R;
import adapter.FragmentDrawer;
import hotchemi.android.rate.AppRate;
import hotchemi.android.rate.OnClickButtonListener;
import model.Arrets;
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
public static Arrets arrets = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppRate.with(this)
.setInstallDays(5) // default 10, 0 means install day.
.setLaunchTimes(10) // default 10
.setRemindInterval(1) // default 1
.setShowNeutralButton(true) // default true
.setDebug(false) // default false
.setOnClickButtonListener(new OnClickButtonListener() // callback listener.
@Override
public void onClickButton(int which)
Log.d(MainActivity.class.getName(), Integer.toString(which));
)
.monitor();
// Show a dialog if meets conditions
AppRate.showRateDialogIfMeetsConditions(this);
arrets = new Arrets();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
View searchContainer = findViewById(R.id.search_container);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
drawerFragment.setFocusableInTouchMode(false);
// display the first navigation drawer view on app launch
displayView(0);
@Override
public void onBackPressed()
super.onBackPressed();
new AlertDialog.Builder(this)
.setMessage("Voulez-vous quitter l\'application ?")
.setPositiveButton("oui", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
).setNegativeButton("non", null).show();
@Override
public void onDrawerItemSelected(View view, int position)
displayView(position);
private void displayView(int position)
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position)
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new ArretsFragment();
title = "Arrets";
break;
case 2:
fragment = new AboutFragment();
title = "A propos";
break;
case 3:
fragment = new SettingsFragment();
title = "Paramètres";
break;
default:
break;
if (fragment != null)
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
我有 HomeFragment.java :
package activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.pierre.tan.R;
import java.util.List;
import adapter.CustomListAdapter;
import model.Arrets;
import util.Spfav;
public class HomeFragment extends Fragment
ListView favoriteList;
Spfav sharedPreference;
List<Arrets> favorites;
Activity activity;
CustomListAdapter productListAdapter;
public HomeFragment()
// Required empty public constructor
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
activity = getActivity();
@Override
public void onActivityCreated(Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_home, container, false);
View searchContainer = getActivity().findViewById(R.id.search_container);
final EditText toolbarSearchView = (EditText) getActivity().findViewById(R.id.search);
ImageView searchClearButton = (ImageView) getActivity().findViewById(R.id.search_clear);
searchClearButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
toolbarSearchView.setText("");
);
searchContainer.setVisibility(View.GONE);
sharedPreference = new Spfav();
favorites = sharedPreference.getFavorites(activity);
if (favorites == null)
Toast.makeText(getActivity(), "Null", Toast.LENGTH_SHORT).show();
else
if (favorites.size() == 0)
Toast.makeText(getActivity(), "Null", Toast.LENGTH_SHORT).show();
favoriteList = (ListView) view.findViewById(R.id.list);
if (favorites != null)
productListAdapter = new CustomListAdapter(activity, favorites);
favoriteList.setAdapter(productListAdapter);
favoriteList.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
TextView textView = (TextView) view.findViewById(R.id.lieu);
String text = textView.getText().toString();
TextView textView2 = (TextView) view.findViewById(R.id.arret);
String libelle = textView2.getText().toString();
Intent i = new Intent(getActivity(), TempsActivity.class);
i.putExtra("text", text);
i.putExtra("libelle", libelle);
startActivity(i);
);
favoriteList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
@Override
public boolean onItemLongClick(
AdapterView<?> parent, View view,
int position, long id)
ImageView button = (ImageView) view
.findViewById(R.id.imgbtn_favorite);
String tag = button.getTag().toString();
sharedPreference.removeFavorite(activity,
favorites.get(position));
button.setTag("grey");
button.setImageResource(R.drawable.ic_heart_white);
favorites.remove(favorites.get(position));
Toast.makeText(
activity,
activity.getResources().getString(
R.string.remove_favr),
Toast.LENGTH_SHORT).show();
sharedPreference.saveFavorites(activity, favorites);
productListAdapter.notifyDataSetChanged();
return true;
);
// Inflate the layout for this fragment
return view;
public void onAttach(Activity activity)
super.onAttach(activity);
public void onDetach()
super.onDetach();
而且我不知道该怎么做:当我在片段中(只是 Homefragment)时,将一个新功能设置为后按,并且不执行活动的功能(谁为其他片段工作) .
对不起,我的英语不好,如果有人可以帮助我,那就太好了!
【问题讨论】:
【参考方案1】:在你的活动中试试这个:
public boolean onKeyDown(int keyCode, KeyEvent event)
Log.d(TAG, "onkeydown");
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
//your code here
return true;
return super.onKeyDown(keyCode,event);
片段有自己的onBackPressed()
处理程序,它被调用而不是活动的处理程序。这样做是为了返回之前显示的片段,例如按下返回。
【讨论】:
以上是关于Android的后退按钮与Fragment和他的活动不同的动作的主要内容,如果未能解决你的问题,请参考以下文章
Android Fragment 在按下后退按钮时未保存 UI 状态
Fragment 回退栈 传递参数,点击切换图片使用Fragment ListView