I'm using two classes. One for the background service where i'm doing several of gatherings of information, and the other I decided to use a swipe tab view to manage and organize my information/data. In the background service (like I said) i'm retrieving information (mostly string types) and sending them via intent to the Main Activity, this activity receives it via a broadcastreceiver (this part works fine). Now the problem i'm having is that when i try to distribute the information the the diferent tabs, it comes repeated and doesn't seem to refresh now my most crucial problem is the reapeating information part. Going to place my code and see if any one of you brilliant minds :D can help me. I hope you can.
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ActivityMain extends FragmentActivity implements
ActionBar.TabListener {
int NUM_TAB = 3;
static TextView InfoView1;
static TextView InfoView2;
static TextView InfoView3;
static String infoData1 = "";
static String infoData2 = "";
static String infoData3 = "";
Fragment frag;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
Defining the intent
intent = new Intent(this, BroadcastService.class);
}
Starting the broadcastreceiver to call the Background service
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* 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 DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch (position)
{
case 0:
frag = new Information1();
Bundle arg0 = new Bundle();
arg0.putString(Information1.ARG_SECTION, infoData1);
frag.setArguments(arg0);
break;
case 1:
frag = new Information2();
Bundle arg1 = new Bundle();
arg1.putString(Information2.ARG_SECTION, infoData2);
frag.setArguments(arg1);
break;
case 2:
frag = new Information3();
Bundle arg2 = new Bundle();
arg2.putString(BatteryStatus.ARG_SECTION, infoData3);
frag.Information3(arg2);
break;
default:
frag = null;
break;
}
return frag;
}
@Override
public int getCount() {
// Show 3 total pages.
return NUM_TAB;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
private void updateUI(Intent intent) {
String stuff1 = intent.getStringExtra("stuff");
String stuff2 = intent.getStringExtra("stuff2");
String stuff3 = intent.getStringExtra("stuff3");
String stuff4 = intent.getStringExtra("stuff4");
String stuff5 = intent.getStringExtra("stuff5");
String stuff6 = intent.getStringExtra("stuff6");
so on...
And storing them in a string
infoData1 += ("Stuff1: " + stuff1 + "\n");
infoData1 += ("Stuff2: " + stuff2 + "\n");
infoData2 += ("Stuff3: " + Stuff3 + "\n");
infoData2 += ("Stuff4: " + Stuff4 + "\n");
infoData3 += ("Stuff5: " + Stuff5 + "\n");
infoData3 += ("Stuff6: " + Stuff6 + "\n");
}
public static class Information1 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information1(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info1View = inflater.inflate(
R.layout.information1, container, false);
InfoView1 = (TextView) info1View
.findViewById(R.id.info1);
InfoView1.setText(getArguments().getString(ARG_SECTION));
Log.d("InfoView1", infoData1);
return info1View;
}
}
public static class Information2 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information2(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info2View = inflater.inflate(
R.layout.Information2, container, false);
InfoView2 = (TextView) info2View
.findViewById(R.id.info2);
InfoView2.setText(getArguments().getString(ARG_SECTION));
Log.d("InfoView2", infoData2);
return info2View;
}
}
public static class Information3 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information3() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info3View = inflater.inflate(
R.layout.Information3, container, false);
InfoView3 = (TextView) info3View
.findViewById(R.id.info3);
InfoView3.setText(getArguments().getString(ARG_SECTION));
Log.d("BatteryStatView", infoData3);
return info3View;
}
}
@Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter(BroadcastService.BROADCAST_ACTION));
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
}
Any ideas?! =D