Improving android Fragment animation performance - android-fragments

I have an activity with a create account button. On clicking that button it performs a slide up animation. It is slow and choppy. I have tested this on the nexus 4 emulator, as well as a Galaxy s7 edge. I've tried running it in asynctask however it did not improve the animation.
Is there a way I can improve animation performance?
Create Account button onclick method:
public void createAccount(View v) {
Log.i("Login", "Create Account button tapped.");
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_up, 0);
transaction.add(R.id.create_account_frame_layout, new CreateAccountFragment()).commit();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}.execute();
}
CreateAccountFragment class:
import android.support.v4.app.Fragment;
public class CreateAccountFragment extends Fragment {
public CreateAccountFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.create_account_background, container, false);
return rootView;
}
}

Turns out my problem wasn't with the animation. The reason for the poor animation performance was because I only provided one image for the background of all my applications.
Because of this, the image was resizing- an expensive process.
I corrected the issue by providing multiple background images, resized for each dpi in the mipmap folders.

Related

BottomSheetDialogFragment with rounded corners and fragment container

I trying to create BottomSheetDialogFragment with rounded corners which contain fragment container.
I did the correct drawable and apply to xml of this fragment.
code:
public class ContainerFragment extends BottomSheetDialogFragment {
#Override
public void setupDialog(Dialog dialog, int style) {
View view = View.inflate(getContext(), R.layout.fragment_delivery_container, null);
dialog.setContentView(view);
// тут делаю так, чтобы фон под удалёнными краями из-за скругления был прозрачным
((View) view.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
/*getChildFragmentManager()
.beginTransaction()
.add(R.id.container, ProvidersFragment.createInstance(), "TASGGGG")
.commit();*/
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog bDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
...
return bDialog;
}
/* #Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.fragment_delivery_container, null);
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, ProvidersFragment.createInstance(), "TASGGGG")
.commit();
return view;
}*/
....
}
In the current implementation of the code, everything is fine (corners are rounded), only the fragment transaction is commented out. If i'll uncomment, it will throw an error that the "fragment does not have a View". Accordingly, if i'll uncomment onCreateView, then edges of this View appear and I can not do anything with this

Viewpager tabs recreated?

I have implemented a NavigationDrawer with a few items in it. Every item represents a different Fragment and one of them has tabs. My problem is that every time I open this Fragment the tabs reloaded! and added to the previous tabs. Why is this happening and how can I solve it?
This is the Fragment with the tabs:
public class fragment_profilo_tabs extends Fragment implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
String[] tabs = { "Profilo aziendale", "Credenziali" };
/* (non-Javadoc)
* #see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
// Initilization
View view = View.inflate(getActivity(), R.layout.profilo_tabs, null);
viewPager = (ViewPager) view.findViewById(R.id.pager);
actionBar = getActivity().getActionBar();
mAdapter = new TabsPagerAdapter(getFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
return view;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
And this is my ViewPagerAdapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
/* (non-Javadoc)
* #see android.support.v4.view.PagerAdapter#getItemPosition(java.lang.Object)
*/
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new fragment_profilo_modificaProfilo();
case 1:
// Games fragment activity
return new fragment_profilo_credenzialiAccesso();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
Ok there are multiple issues. First and foremost lets talk about your ViewPagerAdapter:
The first thing that caught my eye is this:
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
This is very bad for performance. I know that some posts on Stack Overflow suggest using this to "fix" a few things, but that is not the way to go. Remove this, you don't need it.
But the main issue is in your Fragment. First and foremost this:
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab()
.setText(tab_name)
.setTabListener(this));
}
You are adding tabs to the ActionBar. And the ActionBar is not part of you Fragment. Each time you display this Fragment you add the same tabs to the ActionBar but you never remove them again or anything.
My advice: Don't do this. Open a new Activity for the tabs Fragment. This should clearly not be in the same Activity. I cannot imagine any situation where suddenly adding tabs to the current Activity would satisfy the Android Platform Guidelines or provide good usability.
The general rule is that everything that has to do with the Activity itself - like things concerning the ActionBar or tabs in the ActionBar - should be handled in the Activity. Code which adds tabs to the ActionBar has no place inside a Fragment. So either add the tabs permanently in onCreate() of your Activity. Or create a new Activity with those tabs especially for the fragment_profilo_tabs.
As an aside: class names should never be Snake Case. Start class names with an uppercase letter and use Camel Case. Everything else will just confuse other programmers looking at your code

ViewPager designed to work in Portrait mode recreates fragments on orientation change

I have a rather strange issue while working with ViewPager fragments and orientation change.
My problem in the most simplistic description is as follows:
My application has one activity (MainActivity) that is designed to work in both Portrait and Landscape mode. I am using different layouts in different orientations - A ViewPager in portrait and a simple fragment with a completely different view in landscape. Both these components are independent of each other.
The fragment code I am using in both my ViewPager fragments and my landscape fragment are simple - an onAttach(), onDetach() and onCreateView() where I inflate my view are overridden.
I am not really concerned about retaining fragments when I change orientation as I am showing a completely different layout. However, my issue is that the ViewPager fragments are destroyed and recreated (onDestroy() -> onDetach() -> onAttach() -> onCreate() -> onCreateView()) in landscape mode even when not visible.
Is there a workaround to not recreate these fragments on orientation change?
My MainActivity's onCreate() code is as follows and I am checking orientation in onCreate() to decide what view components need to be intialized.
private ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setLogo(R.drawable.empty);
actionBar.setTitle(R.string.empty);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
mPager = (ViewPager) findViewById(R.id.portraitviewpager);
if (mPager != null) {
mPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager()));
mPager.setPageTransformer(false, new DepthPageTransformer());
mPager.setCurrentItem(HOME);
}
}
}
Also, just to make things more simple, my SectionsPagerAdapter code is as follows:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private static final int PAGE_COUNT = 2;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case MAIN_MENU:
return MainMenuFragment.newInstance();
case HOME:
return HomeFragment.newInstance();
}
return null;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
Add setRetainInstance(true); to the onAttach(Activity activity) of the ViewPager fragments.
Open for correction, as Always!
Regards,
Edward Quixote.

How to play video through FragmentTabHost in android

I made an application that has 4 tabs:
vedio tab, in this tab I want to play video, taking remote url
virtual lab
edit video
help
I have made MainActivity class in which use FragmentTabHost class id for display tab.
public class MainActivity extends FragmentActivity{
private FragmentTabHost mTabHost;
private FragmentTabHost mTabHostabove;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Vedio").setIndicator("Vedio",getResources().getDrawable(R.drawable.ic_launcher)),FragmentTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("Virtual Lab").setIndicator("Virtual Lab"),FragmentTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("Edit Vedio").setIndicator("Edit Vedio"),FragmentTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("Help").setIndicator("Help1"),FragmentTab.class,null);
mTabHostabove = (FragmentTabHost)findViewById(R.id.tabhostabove);
mTabHostabove.setup(this, getSupportFragmentManager(), R.id.tabcontent);
mTabHostabove.addTab(mTabHostabove.newTabSpec("Logo").setIndicator("Logo"),FragmentTab.class,null);
mTabHostabove.addTab(mTabHostabove.newTabSpec("Vedio Url ").setIndicator("Vedio Url",getResources().getDrawable(R.drawable.ic_launcher)),FragmentTab.class,null);
}
}
I have also made FragmentTab class which extends Fragment.
here is code:
public class FragmentTab extends Fragment {
private TextView tv;
private VideoView mVideoView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
tv = (TextView) v.findViewById(R.id.text);
mVideoView = (VideoView)v.findViewById(R.id.vedioview);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
String path1="http://commonsware.com/misc/test2.3gp";
MediaController mc = new MediaController(getActivity());
mc.setAnchorView(mVideoView);
mc.setMediaPlayer(mVideoView);
mVideoView.setMediaController(mc);
mVideoView.requestFocus();
// mVideoView.setVideoURI(Uri.parse("android.resource://" +getActivity().getApplicationContext().getPackageName() +"/"+R.raw.song));
mVideoView.setVideoURI(Uri.parse(path1));
mc.show();
mVideoView.start();
} catch (Exception e) {
}
}
});
String tag = this.getTag();
if (tag == "Vedio") {
tv.setText("play vedio");
}
if (tag == "Edit Vedio") {
tv.setText("want to Edit Vedio !!!!!!!");
}
if (tag == "Help") {
tv.setText("do u want help !!!!!!!");
}
if (tag == "Virtual Lab") {
tv.setText("Enter Virtual lab !!!!!!!");
}
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
I am stuck with to play vedio on click vedio tab
anybody solve it if u can . I have searched for 3 days, but i have not found a solution. vedio sound is coming, but vedio is not playing.
Not all devices support all video codecs.
I had the same problem: I work with mp4-format and a resolution of 1280x720. This video format wasn't support by all devices (e.g. HTC Wildfire S). After changing the resolution to 480x360 the video was shown by all devices.
This link should help you:
http://developer.android.com/guide/appendix/media-formats.html

How to start child Activity from Fragment?

I am using Fragment activity in place of tab Group Activity. like this.
public class TabGroup1Activity extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
Intent intent = new Intent(getActivity(), HomeActivity.class);
getActivity().startActivity(intent);
return (RelativeLayout) inflater.inflate(R.layout.home, container,
false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
it starting activity not as child (starting in full screen)
but I don't know how to start as child.
please help me...
Activities are designed to take up the whole screen, while Fragments are designed to take up all, some, or none of the screen. You shouldn't be extending Fragment for your Activity, do this instead.
public class TabGroup1Activity extends FragmentActivity {
You do not want to start an Activity inside your FragmentActivity, you need to create a new Fragment and add it to a layout container in your TabGroup1Activity's layout, using getSupportFragmentManager() or getFragmentManager() with a FragmentTransaction.
See http://developer.android.com/training/basics/fragments/creating.html

Resources