I had a very similar problem before where i wanted to show random xml layouts. I've done that - with lots of help by Ben Williams - with a class named DisplayRandomPage.class and I had a main.xml layout with four buttons.
That's the code of the Main.class:
switch (view.getId()) {
case R.id.first_button:
startActivity(new Intent(this, FirstPage.class));
break;
case R.id.second_button:
startActivity(new Intent(this, SecondPage.class));
break;
case R.id.third_button:
startActivity(new Intent(this, ThirdPage.class));
break;
case R.id.random_button:
Intent intent = new Intent(this, DisplayRandomPage.class);
startActivity(intent);
and this is in the DisplayRandomPage.class:
public class DisplayRandomPage extends Activity {
private Integer [] mLinearLayoutIds = {
R.layout.one
R.layout.two,
R.layout.three
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Random random = new java.util.Random();
int rand = random.nextInt(3);
setContentView(mLinearLayoutIds[rand]);
}
}
What i'd like to do is creating a DisplaySpecificPage.class. Above I've shown my main.class with the switch-case-clause. So when i click on the first button, it will start the FirstPage.class, clicking the second, it will start SecondPage.class, and so on. So for each xml-file i have to create a new java-class although the different java-classes do all the same. Only the xml-files are different. So i'd like to put something like this:
pseudo-code:
case R.id.first_button:
startActivity(new Intent(this, DisplaySpecificPage.class)) with R.layout.first_page;
break;
how do i pass the ID from the layout (R.layout.first_page) on?
You should change your switch statement to
final Intent intent = new Intent(this, DisplaySpecificPage.class);
switch (view.getId()) {
case R.id.first_button:
intent.putExtra("mylayout", R.layout.one);
break;
case R.id.second_button:
intent.putExtra("mylayout", R.layout.two);
break;
case R.id.third_button:
intent.putExtra("mylayout", R.layout.three);
break;
case R.id.random_button:
intent.putExtra("mylayout", randomNumber);
startActivity(intent);
}
startActivity(intent);
This way you'd start the same activity no matter which button would be pressed, and inside the DisplaySpecificPage activity's onCreate method you should set the content to this passed layout:
final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
setContentView(layout);
The code above passes an extra parameter to the intent when starting the DisplaySpecificPage activity, with the name: "mylayout".
Inside the DisplaySpecificPage class' onCreate method you just retrieve this extra parameter using the getIntExtra method of the passed intent (getIntent() will return it for you), and you set the content view of this DisplaySpecificPage activity to the passed layout by setContentView(layout).
You should make sure though, to always pass a valid layout identifier to that intent, otherwise you'll get exception when trying to inflate it (so randomNumber should be selected properly).
Update
With adding extras to the Intent you can parametrize your activities.
So using intent.putExtra("paramName", paramValue) you'll pass the paramValue value on the name of paramName to the activity you start by this intent.
You want to start the DisplaySpecificPage activity, but want it to have different layout based on which button you click.
So you create an intent:
final Intent intent = new Intent(this, DisplaySpecificPage.class);
Before starting the activity by calling startActivity(intent), you have to put this extra information inside the intent, so the DisplaySpecificPage activity to know which layout it should set as its content view:
So if you pressed the second button, you want the layout of your new activity to be the one defined by the two.xml inside your res/layout folder. It is referenced by as R.layout.two (which is a static int value).
intent.putExtra("mylayout", R.layout.two);
This line puts the layout's value as an extra into the intent object, and now you can start the activity, the layout reference will be passed to it.
The "mylayout" is a name you choose for your parameter. It can be anything (valid), it will be used inside the DisplaySpecificPage activity to retrieve the layout's reference. It is retrieved by this name:
final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
The getIntExtra method gets the integer value from the intent which has the name "mylayout", and if it's not found, then it will return R.layout.one.
If you want to handle this case (when no parameter with name mylayout is set), you can write
final int layout = getIntent().getIntExtra("mylayout", -1);
if (layout < 0)
{
//TODO: no layout reference was passed
}
Here is my final code:
Main.class:
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void change(final View view) {
Intent intent2 = null;
final Intent intent = new Intent(this, DisplaySpecificPage.class);
switch (view.getId()) {
case R.id.first_button:
intent.putExtra("mylayout", R.layout.layout1);
break;
case R.id.second_button:
intent.putExtra("mylayout", R.layout.layout2);
break;
case R.id.third_button:
intent.putExtra("mylayout", R.layout.layout3);
break;
case R.id.random_button:
intent2 = new Intent(this, DisplayRandomPage.class);
startActivity(intent2);
}
// if the Random button was not clicked, if-condition is true.
if (intent2 == null)
startActivity(intent);
}
}
DisplaySpecificPage.class:
public class DisplaySpecificPage extends Activity{
public void onCreate(Bundle savedInstanceState) {
final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
super.onCreate(savedInstanceState);
setContentView(layout);
}
}
DisplayRandomPage.class:
public class DisplayRandomPage extends Activity {
private Integer [] mLinearLayoutIds = {
R.layout.layout1,R.layout.layout2,R.layout.layout3
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Random random = new java.util.Random();
int rand = random.nextInt(mLinearLayoutIds.length);
setContentView(mLinearLayoutIds[rand]);
}
}
Big thanks to rekaszeru!
Related
I'm a noobie making a quiz in Android Studio and i'm trying to pass an integer between activities to add to the amount of questions they got correct for the end but in the second activity it isn't changing when I answer the first question correct.
Question1 activity:
public class Question1 extends AppCompatActivity {
public int correctAnswers = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
Intent intent = new Intent(Question1.this, Question2.class);
Intent i = getIntent();
Intent answersCorrect = new Intent(Question1.this, Question2.class);
answersCorrect.putExtra("correctAnswers", correctAnswers);
}
public void submitQuestion1(View view) {
EditText question1TextInput = (EditText) findViewById(R.id.question1TextInput);
if (question1TextInput.getText().toString().length() >= 1) {
startActivity(new Intent(Question1.this, Question2.class));
if (question1TextInput.getText().toString().toUpperCase().contentEquals("FATHER")) {
correctAnswers += 1;
Intent answersCorrect = new Intent(Question1.this, Question2.class);
answersCorrect.putExtra("correctAnswers", correctAnswers);
}
}
}
}
Question2 Activity:
public class Question2 extends AppCompatActivity {
public int correctAnswers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
Intent intent = getIntent();
int number = intent.getIntExtra("correctAnswers", 0);
TextView myAwesomeTextView = (TextView)findViewById(R.id.text);
myAwesomeTextView.setText(String.valueOf(number));
}
}
you duplicate your intent here:
Intent intent = new Intent(Question1.this, Question2.class);
Intent i = getIntent();
Intent answersCorrect = new Intent(Question1.this, Question2.class);
answersCorrect.putExtra("correctAnswers", correctAnswers);
replace it to:
Intent intent = new Intent(Question1.this, Question2.class);
intent.putExtra("correctAnswers", correctAnswers);
startActivity(intent);
in your second Activity:
int correctAnswers;
correctAnswers = (int) getIntent().getIntExtra("correctAnswers", 0);
So basically, when you have one activity and want to open a second activity, an Intent is the most important thing to have. It's responsible for communcation between your system and your application.
Intent is responsible for starting an activity, starting a service, and delivering a broadcast.
Note that there are two different types of intent: Explicit and Implicit.
Explicit Intent is used in this manner:
You have Activity_1 and you KNOW that you want to start Acticity_2 FROM Activity_1.
Implicit Intent is used when you DON'T know the name of the activity that you want to start.
Now, I know you probably understand what the StartActivity() method DOES, but StartActivity always requires an intent to go into the parenthesis. StartActivity(Activity_2); will not work.
So, when using Explicit Intent:
Intent i = new Intent(Activity_1.this, Activity_2.class);
StartActivity(i);
You start with making a reference - i - and, inside the parameters, the first being the activity from which you are calling the second activity, and the second being the activity which you want to call.
Here's a video on Intents as well: https://youtu.be/FH1Ym1KjJNc
Hope this helped.
Move the intent to a field. You only need it once.
Then, the issue is that you start the other activity too soon, without setting any value. You started the other activity with an empty, new Intent
public class Question1 extends AppCompatActivity {
public int correctAnswers = 0;
final Intent answersIntent = new Intent(Question1.this, Question2.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
public void submitQuestion1(View view) {
EditText question1TextInput = (EditText) findViewById(R.id.question1TextInput);
String answer = question1TextInput.getText().toString();
// No need to check for length if directly checking another string
if (answer.toUpperCase().contentEquals("FATHER")) {
answersIntent.putExtra("correctAnswers", ++correctAnswers);
startActivity(answersIntent);
}
}
If you plan on sharing that value over many questions, try SharedPreferences.
FWIW, make a generic View for any question that has the question text and possible answer fields. Try not to make one activity per question.
Is it possible to define the source of a click? I can access my MainActivity through either clicking on a RecyclerView or through a Notification action. Depending on which it is, I need to provide different info. Is there a way of saying: if click is from recyclerview then..., else if it is from notification action then...?
What I can think of so far is this, but the problem is I am not using buttons as such:
Button mClickButton1 = (Button)findViewById(R.id.clickButton1);
mClickButton1.setOnClickListener(this);
Button mClickButton2 = (Button)findViewById(R.id.clickButton2);
mClickButton2.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.clickButton1: {
// do something for button 1 click
break;
}
case R.id.clickButton2: {
// do something for button 2 click
break;
}
}
}
Thanks!
you have to define two different calling intents for the same activity and put info for each View Example :
mClickButton1.setOnClickListener(new onClickListener(){
public void onClick(View v) {
Intent view1_int = new Intent (this, MainActivity.class);
view1_int.putExtra("Calling Intent" ,"RecyclerView");
startaActivityForResult(view1_int);
}
});
mClickButton2.setOnClickListener(new onClickListener(){
public void onClick(View v) {
Intent view2_int = new Intent (this, MainActivity.class);
view1_int.putExtra("Calling Intent" ,"Notification action");
startaActivityForResult(view1_int);
}
});
and in the onCreate Method in your MainActivity you can say :
String callin_view;
callin_view =getresources.getIntent.getExtras("Calling_Intent");
This will retrieve the name of the calling source you defined
I'm having problem in storing the intent.putExtra inside the android local database. I am creating a game like 4Pics1Word for my project. It has only 25 levels so I created 25 activities and I randomized it. After solving a particular activity, it will then be removed to the ArrayList of Classes, ArrayList<Class>. Now, I used intent.putExtra("ACTIVITY_LIST", activityList); to store the intent and to pass it to the next activity. My problem is I can't store it on local database. When I exit the game the progress is not saved, it starts again from the first level. Any suggestions? Thank you!
Here's my code in my Main Activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btnStart;
Context context;
SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// We are creating a list, which will store the activities that haven't been opened yet
ArrayList<Class> activityList = new ArrayList<>();
activityList.add(first.class);
activityList.add(second.class);
activityList.add(third.class);
activityList.add(fourth.class);
activityList.add(fifth.class);
Random generator = new Random();
int number = generator.nextInt(5) + 1;
Class activity = null;
switch(number) {
case 1:
activity = first.class;
activityList.remove(first.class);
break;
case 2:
activity = second.class;
activityList.remove(second.class);
break;
case 3:
activity = third.class;
activityList.remove(third.class);
break;
case 4:
activity = fourth.class;
activityList.remove(fourth.class);
break;
default:
activity = fifth.class;
activityList.remove(fifth.class);
break;
}
Intent intent = new Intent(getBaseContext(), activity);
intent.putExtra("ACTIVITY_LIST", activityList);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(activityList); // myObject - instance of MyObject
prefsEditor.putString("MyObject", json);
prefsEditor.commit();
startActivity(intent);
}
}
Here's my code in my first activity:
public class first extends AppCompatActivity implements View.OnClickListener{
EditText etAnswer;
Button btnGo;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
etAnswer = (EditText) findViewById(R.id.etAnswer);
btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnGo:
String answer = etAnswer.getText().toString();
if(answer.equals("Jose Rizal") || answer.equals("jose rizal") || answer.equals("Rizal") || answer.equals("rizal") ){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("The famous Rizal monument in Luneta was not the work of a Filipino but a Swiss sculptor named Richard Kissling?" +
"\n" +
"\n" +
"Source: http://www.joserizal.ph/ta01.html");
dlgAlert.setTitle("Did you know that ...");
dlgAlert.setPositiveButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ArrayList<Class> activityList = new ArrayList<>();
Bundle extras = getIntent().getExtras();
activityList = (ArrayList<Class>) extras.get("ACTIVITY_LIST");
if(activityList.size() == 0) {
Context context = getApplicationContext();
CharSequence last = "Congratulations! You just finished the game! Please wait for the next update!";
int durationFinal = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, last, durationFinal);
toast.show();
} else {
// Now, the random number is generated between 1 and however many
// activities we have remaining
Random generator = new Random();
int number = generator.nextInt(activityList.size()) + 1;
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// We will open the first remaining activity of the list
activity = activityList.get(0);
// We will now remove that activity from the list
activityList.remove(0);
break;
case 2:
// We will open the second remaining activity of the list
activity = activityList.get(1);
activityList.remove(1);
break;
case 3:
// We will open the third remaining activity of the list
activity = activityList.get(2);
activityList.remove(2);
break;
case 4:
// We will open the fourth remaining activity of the list
activity = activityList.get(3);
activityList.remove(3);
break;
default:
// We will open the fifth remaining activity of the list
activity = activityList.get(4);
activityList.remove(4);
break;
}
// Note: in the above, we might not have 3 remaining activities, for example,
// but it doesn't matter because that case wouldn't be called anyway,
// as we have already decided that the number would be between 1 and the number of
// activities left.
// Starting the activity, and passing on the remaining number of activities
// to the next one that is opened
Intent intent = new Intent(getBaseContext(), activity);
intent.putExtra("ACTIVITY_LIST", activityList);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}else{
Context context = getApplicationContext();
CharSequence text = "Wrong! Try Again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
}
}
}
Everytime you come to MainActivity , you new an ArrayList and add all the activities rather than get the cache from your local SharedPreferences .
When you finish one game in an Activity , you did not save your progress in cache.
After updating the arrayList,save your arrayList like this :
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(activityList); // myObject - instance of MyObject
prefsEditor.putString("MyObject", json);
prefsEditor.commit();
And when you want to read the arrayList saved ,do like this:
String arrayStr = mPrefs.getString("myObject","defValue");
Gson gson = new Gson();
List<Class> array = gson.fromGson(arrayStr,new TypeToken<List<Class>>(){}.getType());
if(array==null){
array = new ArrayList<>();
array.add(...);
}
I am working on an android app that launches two activities using the on click listener everything in my code checks out fine except where the public void onClick(View v) begins I have multiple errors starting on that line and I am unable to run the code? I would kindly appreciate any help as I am fairly new to this. My code is as follows
public class Safaricom extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.safaricom);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(buttonClickListener);
button2.setOnClickListener(buttonClickListener);
}
private OnClickListener buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = null;
switch(v.getId()){
case R.id.button1:
intent = new Intent(this, Second.class);
break;
case R.id.button2:
intent = new Intent(this, SignUp.class);
break;
}
if (intent != null)
this.startActivity(intent);
}
};
}
The Errors are at two points.
First where it says public void on click view ( The Error is - Multiple Markers at this line - implements android.view.View.OnClickListener.onClick- The method onClick(View) of type new View.OnClickListener(){} must override a superclass )
Second where it says this.startActivity(intent); (The Error is -The method startActivity(Intent) is undefined for the type new View.OnClickListener(){})
Instead of this use v.getContext() or YOUR_ACTIVITY.this
Actually If you read the Docs carefully, you will know that Intent parameters contain Activity so when you are using this it means that you are giving a parameter of type new View.OnClickListener
Well, I can see right off a couple of errors. To make it clearer since it apparently was not clear by simply looking at the code and learning. I added Safaricom.this in each of the new Intent statements. This is because the Intent constructor needs a Context as the first argument and an OnClickListener is not a Context, you need to ge the enclosing Activity which is a context. One other edit, I missed, the startActivity also needs to have Safaricom prepended.
public class Safaricom extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.safaricom);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(buttonClickListener);
button2.setOnClickListener(buttonClickListener);
}
private OnClickListener buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = null;
switch(v.getId()){
case R.id.button1:
intent = new Intent(Safaricom.this, Second.class);
break;
case R.id.button2:
intent = new Intent(Safaricom.this, SignUp.class);
break;
}
if (intent != null)
Safaricom.this.startActivity(intent);
}
};
}
For the first error
The Error is - Multiple Markers at this line - implements android.view.View.OnClickListener.onClick- The method onClick(View) of type new View.OnClickListener(){} must override a superclass )
Try removing the #override
If that doesn't remove the second error then let us know if there is a different issue arising.
I have a TabActivity that has a TabHost with two tabs. Each tab has its own intent. It seems like the intent's onResume() fires before I can detect if a tab was changed. How can I resolve this?
TabActivity code:
public class TabHostActivity extends TabActivity {
static final int SHOW_SHARE_ACTIVITY = 0;
static final int SHOW_LOGIN_ACTIVITY = 1;
private TabHost tabHost;
private ImageButton composeImageButton;
private SharedPreferences prefs;
private Bundle b;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tabhostactivity);
prefs = getSharedPreferences(Constants.PREFS_NAME, 0);
//Setup the ActionBar
composeImageButton = (ImageButton) findViewById(R.id.composeImageButton);
composeImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(prefs.getBoolean("isLoggedIn", false))
{
showShareActivity();
}
else
{
Intent intent = new Intent(TabHostActivity.this, LoginActivity.class);
startActivityForResult(intent, SHOW_LOGIN_ACTIVITY);
}
}
});
b = new Bundle();
//Setup the Tabs
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
if(tabHost.getCurrentTab() == 0) //Check if the Watchlist tab was clicked so we can prompt login
{
//Toast toast = Toast.makeText(getApplicationContext(), "TRENDING = YES", Toast.LENGTH_SHORT);
//toast.show();
b.putBoolean("isTrendingTab",true);
}
else
{
Toast toast = Toast.makeText(getApplicationContext(), "TRENDING = NO", Toast.LENGTH_SHORT);
toast.show();
b.putBoolean("isTrendingTab",false);
}
}
});
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ARActivity.class);
intent.putExtras(b);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("trending").setIndicator("Trending",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, WatchlistActivity.class);
intent.putExtras(b);
spec = tabHost.newTabSpec("watchlist").setIndicator("Watchlist",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
private void showShareActivity()
{
Intent intent = new Intent(TabHostActivity.this, ShareActivity.class);
startActivityForResult(intent, SHOW_SHARE_ACTIVITY);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == SHOW_LOGIN_ACTIVITY)
{
//Login was successful so lets show the compose box!
if (resultCode == RESULT_OK) {
showShareActivity();
}
}
}
}
Here is the onResume in the Intent for one of my Activities:
public void onResume()
{
super.onResume();
Bundle bundle = getIntent().getExtras();
if(bundle.getBoolean("isTrendingTab"))
{
Toast toast = Toast.makeText(getApplicationContext(), "TRENDING!", Toast.LENGTH_SHORT);
toast.show();
}
else
{
Toast toast = Toast.makeText(getApplicationContext(), "WATCHLIST!", Toast.LENGTH_SHORT);
toast.show();
}
}
If i understood correctly the problem is that you try to put
b.putBoolean("isTrendingTab",true);
(or false) on the intent you're going to launch by detecting change.
That's the wrong approach.
The change event will always occur after the activity is launched, you should do the logic different. You have to rethink it.
Have you looked at Activity life cycle? The resume is being called when the activity is being created too and the line bundle.getBoolean("isTrendingTab") does not have a default value in case it has not been set yet...
Can you set it first in the onCreate to a default value? I think that is your issue. The code is a little sloppy. You are trying to pass variables to each activity but they still both exists in the tab activity. Views would be a better method so they all see the same variables in the tab activity.
The oncreate of your class ARActivity.class will be called before your onresume method of your tab host.
So do whatever the processing you want in your ARActivity.
Also since your tabHost.setCurrentTab(0); your starting tab will always be ARActivity.
And if you want to activate code depending your tab change, figure out which tab you are on using using the main tabhost ontabchange and use the id and then send a request to a inner broadcast receiver.
if (tabHost.getCurrentTab() == 0) {
i.setAction(getString(R.string.br_refresh_home_tab));
sendBroadcast(i);
} else {
i.setAction(getString(R.string.br_refresh_sports_tab));
sendBroadcast(i);
}
In your ARActivity,
protected class RefreshList extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
getString(R.string.br_refresh_home_tab))) {
}
}
}
There's no need to use onTabChanged().
Here's how I do it in my app (with some of your values pasted in). I add the boolean flag to the intent, not an extra bundle:
Intent intent = new Intent(action) // see notes below about "action"
.setClass(this, ARActivity.class)
.putExtra("isTrendingTab", true);
TabHost.TabSpec spec = tabHost.newTabSpec("trending")
.setIndicator("trending", getResources().getDrawable(drawableId))
.setContent(intent);
tabHost.addTab(spec);
Then in onResume():
if (getIntent().getBooleanExtra("isTrendingTab", false)) {...
I found that when using the same class for multiple tabs, I had to differentiate them with a different action string in each Intent constructor, as above. Otherwise it wouldn't create a new activity when switching between tabs of the same class. You don't appear to be doing this (yet), so you can continue to leave it out. I thought I'd mention it, since passing isTrendingTab suggests you might be heading down this route.