【问题标题】:Why does Exit Animation not work upon exiting an activity by tapping on back arrow which is on top-left of Toolbar/AppBar?为什么通过点击工具栏/应用栏左上角的后退箭头退出活动时退出动画不起作用?
【发布时间】:2015-10-11 15:53:32
【问题描述】:

我正在开发一个 ma​​terial design 应用程序,并且我已在 MainActivity 中声明了一个退出动画

当我通过单击返回按钮退出时,动画正在运行,但在通过点击工具栏/AppBar 左上角的 返回箭头 退出应用程序时,动画无法运行.

这是MainActivity.java文件的代码:

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    int normalTabTextColor = Color.parseColor("#64B5F6");
    int selectedTabTextColor = Color.parseColor("#2196F3");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // Check if we're running on Android 5.0 or higher
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Call some material design APIs here
            // enable transitions
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        } else {
            // Implement this feature without material design
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.parseColor("#2196F3"));
        setSupportActionBar(toolbar);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setTabTextColors(normalTabTextColor, selectedTabTextColor);
        tabLayout.setupWithViewPager(mViewPager);

        /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });*/

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch (id) {
            case R.id.action_profile:
                // Check if we're running on Android 5.0 or higher
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setExitTransition(new Explode());
                    // Call some material design APIs here
                    Intent profileIntent = new Intent(MainActivity.this, ProfileActivity.class);
                    startActivity(profileIntent,
                            ActivityOptions
                                    .makeSceneTransitionAnimation(this).toBundle());
                } else {
                    // Implement this feature without material design
                    Intent profileIntent = new Intent(MainActivity.this, ProfileActivity.class);
                    startActivity(profileIntent);
                }
                break;
            case R.id.action_support_development:
                // Check if we're running on Android 5.0 or higher
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setExitTransition(new Explode());
                    // Call some material design APIs here
                    Intent supportDevelopmentIntent = new Intent(MainActivity.this, SupportDevelopmentActivity.class);
                    startActivity(supportDevelopmentIntent,
                            ActivityOptions
                                    .makeSceneTransitionAnimation(this).toBundle());
                } else {
                    // Implement this feature without material design
                    Intent supportDevelopmentIntent = new Intent(MainActivity.this, SupportDevelopmentActivity.class);
                    startActivity(supportDevelopmentIntent);
                }
                break;
            case R.id.action_settings:
                // Check if we're running on Android 5.0 or higher
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setExitTransition(new Explode());
                    // Call some material design APIs here
                    Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivity(settingsIntent,
                            ActivityOptions
                                    .makeSceneTransitionAnimation(this).toBundle());
                } else {
                    // Implement this feature without material design
                    Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivity(settingsIntent);
                }
                break;
            case R.id.action_help:
                // Check if we're running on Android 5.0 or higher
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setExitTransition(new Explode());
                    // Call some material design APIs here
                    Intent helpIntent = new Intent(MainActivity.this, HelpActivity.class);
                    startActivity(helpIntent,
                            ActivityOptions
                                    .makeSceneTransitionAnimation(this).toBundle());
                } else {
                    // Implement this feature without material design
                    Intent helpIntent = new Intent(MainActivity.this, HelpActivity.class);
                    startActivity(helpIntent);
                }
                break;
        }

        return super.onOptionsItemSelected(item);
    }


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Accept a Request";
                case 1:
                    return "Post a Request";
            }
            return null;
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
}

我真的不知道这里出了什么问题。

请告诉我!

提前致谢。

【问题讨论】:

    标签: android android-animation material-design


    【解决方案1】:

    你必须在onOptionsItemSelected函数中处理'home'的情况。

    在 switch 中添加一个条目,如下所示:

        switch(id){
                .....
                case android.R.id.home: 
                supportFinishAfterTransition();
                return true;
                .....
        }
    

    基本上当您单击顶部的按钮时,menuitem 的 id 是 android.R.id.home 因此退出动画也应该在这里处理。看看有没有帮助。

    【讨论】:

    • 首先检查您是否能够在该代码中跟踪该事件。如果是,则应用您的动画。检查您正在按下的按钮的 Id。它通常是 Android.R.id.home 或 R.id.home。首先检查事件。
    • 嘿,我只是一个初学者,我听不懂你在说什么!请用通俗易懂的语言解释!
    • @HammadNasir 好的。没问题。看到您在顶部单击的按钮必须有一个 id 对吗??您可以通过使用 onOptionsItemSelected 中的 System.out.println() 打印它来获得这个想法。当您单击返回按钮时,会调用 onOptionsItemSelected 函数。在该函数中,您已经处理了一些案例,例如 R.id.action_profile、R.id.action_settingsR.id.action_support_development、R.id.action_settings 和 R.id.action_help。现在要处理后退按钮,您还必须在 onOptionsItemSelected 函数中为此添加一个条目。按下按钮并打印 id。
    • 使用该 id 并在 switch case 中添加一个条目。每当您按下顶部的后退按钮时,都会调用 onOptionsItemSelected 并且它会查找 id = 您的主页按钮 id 的情况。因此,只需在该案例中添加退出动画代码,它就会开始工作。有什么疑问吗??
    • 添加 supportFinishAfterTransition();返回真;在你的情况下 android.R.id.home。看到这篇文章github.com/codepath/android_guides/wiki/…它现在应该可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多