Sanjeetjh
Sanjeetjh

Reputation: 31

Fragment class for doing some background process

I want To develop android Fragment class for doing some background process for my active. can any one suggest some code.

thanks

Upvotes: 0

Views: 1892

Answers (3)

Croolsby
Croolsby

Reputation: 1476

To add a fragment without a UI, add the fragment from the activity using add(Fragment, String) (supplying a unique string "tag" for the fragment, rather than a view ID). This adds the fragment, but, because it's not associated with a view in the activity layout, it does not receive a call to onCreateView(). So you don't need to implement that method.

Source: http://developer.android.com/guide/components/fragments.html

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(new Fragment(), "fragmentTag");
ft.commit();

Upvotes: 0

GyaniPundit
GyaniPundit

Reputation: 133

Pasting it from developer.google.com

Unless you specify otherwise, most of the operations you do in an app run in the foreground on a special thread called the UI thread. This can cause problems, because long-running operations will interfere with the responsiveness of your user interface. This annoys your users, and can even cause system errors. To avoid this, the Android framework offers several classes that help you off-load operations onto a separate thread running in the background. The most useful of these is IntentService.

http://developer.android.com/training/run-background-service/index.html

Upvotes: 0

SKK
SKK

Reputation: 5271

Background or long running operations of an activity must be carried out in AsyncTask or using a service.

A fragment is basically a portion of user interface in an Activity.

Code samples and more detailed explanations for Asynctask and Services can be found here and here

Upvotes: 2

Related Questions