如何通过调用android中的Fragment中的finish()关闭窗口?

时间:2015-06-17 07:52:27

标签: android

我使用Android Studio 1.2.2向导创建了一个Tabbed Activaity,我希望在单击按钮btnClose时关闭此窗口,我编写代码rootView.getContext().finish();,但这是错误的, PlaceholderFragmentOld.this.finish();也是错的。

我该怎么办?谢谢!

PlaceholderFragmentOld.java

package ui.tab;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.widget.Button;
import com.google.android.gms.ads.AdView;
import info.dodata.messagecleanup.R;
import bll.PublicParFun;
import info.dodata.messagecleanup.R;

public class PlaceholderFragmentOld extends Fragment {

    private AdView adView;

    public PlaceholderFragmentOld() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.cleanup_delete_fragment_old, container, false);

        adView=(AdView) rootView.findViewById(R.id.adView);
        PublicParFun.SetAD(adView);

        SetButtons(rootView);

        return rootView;
    }


    private void SetButtons(final View rootView){
        rootView.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rootView.getContext().finish();
            }
        });

    }


}

CleanupDelete.java

package ui;

import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import info.dodata.messagecleanup.R;
import ui.tab.PlaceholderFragmentOld;
import ui.tab.PlaceholderFragmentPerson;
import ui.tab.PlaceholderFragmentReduce;
import ui.tab.PlaceholderFragmentTrim;

public class CleanupDelete extends ActionBarActivity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cleanup_delete);

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment;
            switch(position){
                case 0:
                    fragment = new PlaceholderFragmentOld();
                    break;
                case 1:
                    fragment = new PlaceholderFragmentReduce();
                    break;
                case 2:
                    fragment = new PlaceholderFragmentTrim();
                    break;
                case 3:
                    fragment = new PlaceholderFragmentPerson();
                    break;
                default:
                    throw new IllegalArgumentException("Invalid section number");
            }

            return fragment;
        }



        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.Tab_Old);
                case 1:
                    return getString(R.string.Tab_Reduce);
                case 2:
                    return getString(R.string.Tab_Trim);
                case 3:
                    return getString(R.string.Tab_Person);
            }
            return null;
        }
    }

}

cleanup_delete.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
    android:layout_width="match_parent" android:layout_height="match_parent"
/>

cleanup_delete_fragment_old.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border_ui" >


    <LinearLayout
        android:id="@+id/linearLayoutMain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:paddingTop="20dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/tVDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dip"
            style="@style/myTextDesciption"
            android:text="This is a descript"/>


    </LinearLayout>



    <LinearLayout
        android:id="@+id/linearLayoutToolBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:weightSum="4" >

        <Button
            android:id="@+id/btnDelete"
            style="@style/myTextMedium"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/BtnDelete" />


        <Button
            android:id="@+id/btnClose"
            style="@style/myTextMedium"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/BtnClose" />

    </LinearLayout>


</RelativeLayout>

4 个答案:

答案 0 :(得分:3)

finish()只能在Activity中调用。鉴于此,你这样做:

// Somewhere in your fragment

private void finishActivity() {
    if(getActivity() != null) {
        getActivity().finish();
    }
}

然后你可以调用该方法来完成你的活动:

// Somewhere in your click listener

finishActivity();

重要提示: getActivity()可能会返回null。当片段与您的Activity分离时(使用ViewPager时或当您在活动中的多个片段之间玩杂耍时),这尤其危险。在未经检查的情况下调用它会使您的代码受到NullPointerException的威胁。

答案 1 :(得分:2)

你可以这样做。

 @Override
            public void onClick(View v) {
                getActivity().finish();
            }

答案 2 :(得分:1)

你应该使用getActivity()。finish();

答案 3 :(得分:0)

为了使用Kotlin从片段移动到活动,请添加以下代码:

if(activity != null) {
   activity?.finish()
}