gpt4 book ai didi

android - Backstack android wear 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:01 24 4
gpt4 key购买 nike

我正在尝试创建一个 android wear 应用程序,它只有一个 Activity ,其中有一个 fragment 。我希望能够根据按钮点击或其他用户操作切换 fragment 。

我可以很好地替换 fragment ;但是,当我从屏幕左侧滑动到屏幕右侧时。该应用程序关闭。我希望这次滑动起到后退按钮的作用,而不是退出应用程序。

主要 Activity

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.wearable.view.WatchViewStub;


public class MainActivity extends FragmentActivity implements FragmentChangeListener {

int fragmentContainerId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);


// instialize the fragment to the loading page
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub watchViewStub) {
fragmentContainerId = R.id.fragmentContainer;

if (watchViewStub.findViewById(R.id.fragmentContainer) != null) {




Fragment1 fragment1= new Fragment1 ();

// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
fragment1.setArguments(getIntent().getExtras());

// Add the fragment to the 'fragment_container'
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer, fragment1).addToBackStack(null);
fragmentTransaction.commit();
}
}
});
}

@Override
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment, fragment.toString()).addToBackStack(null);
System.out.println("Entry count in backstack is: " + fragmentManager.getBackStackEntryCount());
fragmentTransaction.commit();
}
}

fragment 1

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.Button;



public class Fragment1 extends Fragment {


public Fragment1() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment1,
container, false);

Button button1= (Button)view.findViewById(R.id.button1);

button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Fragment2 fragment2= new Fragment2 ();
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.replaceFragment(fragment2);
}
});
return view;
}
}

fragment 2

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.Button;


/**
* A simple {@link Fragment} subclass.
*/
public class Fragment2 extends Fragment {


public Fragment2() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.fragment2, container, false);

return v;
}


}

FragmentChangeListner

import android.support.v4.app.Fragment;


public interface FragmentChangeListener
{
public void replaceFragment(Fragment fragment);
}

当我通过单击按钮从 Fragment1 导航到 Fragment2 时。如何在不退出应用程序的情况下导航回 Fragment1?

最佳答案

一个解决方案是将您的第二个布局包装在一个

android.support.wearable.view.SwipeDismissFrameLayout

然后您可以捕获关闭手势并在父 Activity 内部调用 getFragmentManager().popBackStack();

您可以像这样在 SwipeDismissFrameLayout 上设置监听器:swipeFrame.setDismissEnabled(true);
swipeFrame.setOnDismissedListener(new SwipeDismissLayout.OnDismissedListener() {
@覆盖
public void onDismissed(SwipeDismissLayout swipeDismissLayout) {
getActivity().getFragmentManager().popBackStack();
}
});

关于android - Backstack android wear 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012812/

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