Android Best way to pass values from Tabs to Main TabActivity - android

I have a TabActivity which loads 2 ListActivity in 2 Tabs. When I click on a list item in either of the ListActivity, I want to pass this value back to the TabActivity. What's the best way to do this? I'm thinking of using a BroadcastReceiver. Any thoughts?

consider this illustration
public class MyTabActivity extends TabActivity
{
public void onCreate(Bundle b)
{
//implementation
}
public void setSomeObject(Object someOjbect)
{
//will get an object and act accordinglt
}
}
and in any of your child Activity you would use to set Object like this way:
MyTabActivity myTabParent = (MyTabActivity)this.getParent();
myTabParent.setSomeObject(anyObject);

Pass values using intent.
Bundle b=new Bundle();
Intent i=new Intent(this, AnotherActivity.class);
b.putDouble("data", datavalue);//putting the datavalue
i.putExtras(b);
And receive values in AnotherActivity as
double value = this.getIntent().getDoubleExtra("data", defaultvalue);
Inter Change the lines for both activity and get data from each other.

Still Tab-activity is deprecated.I suggest you to please use Fragments instead of this class and it's provide all your requirements., You can use the v4 support library for these purpose.
Thank You

Agree with Javanator. I did it the BroadcastReceiver way and it works. Tedious but it works.

Related

Activtiy1 to Adaper to Activity2 ,and how to use the Activity1 data in Activtiy2

I have two Activies, named Activtiy1 and Activity2, and one Adapter.
Activity1 use the adapter, and in the adapter, I use intent to jump to Activiy2.
My problem is that Activity2 wants to use the data from Activiy1, but I have no simple way to come through.
I want a simple way to solve this problem.
Options:
When creating an intent, put the data in a bundle http://developer.android.com/reference/android/os/Bundle.html, and add it to the intent.
Take a look at the singleton design pattern. Create the object in one activity and it will be accessibly to the other activity.
If all your data are primitive types, you can use intent.putExtra(..,..);
In activity-1 start activity-2
Intent i = new Intent(activity1.this, activity2.class);
i.putExtra("Key","your data");
StartActivity(i);
and in second activity get data from first activity
#Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.xml);
String data = getIntent.getExtra("key");
Toast.makeText(getApplicationContext(), "This is data"+data, Toast.LENGTH_SHORT).show();
}

Pass value through intent or static variable?

I am wondering about the best way to design / program this:
If I have a Boolean value (let's say whether the user has extra power or not) and I need to pass it from Activity A to B to C. Should I add it to an intent from each activity to another OR should I just store it in a static variable and access it every time?
Its is safer to pass it in the intent. sometimes android kills apps without warning when it needs memory and your static values will not be retained on the other hand intent extras are kept. if you want to push it a little further, use shared preference. its designed using Map data struct so speed will not be a problem.
Android Intents have a putExtra method that you can use to pass variables from one activity to another.
public class ActivityA extends Activity {
...
private void startActivityB() {
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("HAS EXTRA POWER", false);
startActivity(intent);
}
}
public class ActivityB exntends Activity {
Bundle bundle;
private void hasExtraPower() {
bundle = getIntent().getExtras();
if(!bundle.getBoolean("HAS EXTRA POWER")
// do something
else
// do something else
}
}
Passing data through Intent
If you use that only in that activity that's fine but
When u need to pass to other layer like viewmodel that will make your operation's speed slower

How to pass value to another tab without starting Activity?

I have 2 Tabs - Tab1 and Tab2, Tab1Activity and Tab2Activity.
I want to pass values from Tab1Actvity to Tab2Activity but dont want to start Tab2Activity.
When i try below code it gives null value:
In Tab1Activity
getParent().getIntent().putExtra("key", "value");
In Tab2Activity
String valueString=getParent().getIntent().getStringExtra("key");
System.out.println("Testing.....: "+valueString);
I really discourage you from using global variables by extending the Application class. If your application goes to the background, (e.g. due a phone call) the android system might decide to kill your application. When the call is finished your application and the activity stack will be restored, but your activity state will be lost.
I'd rather suggest you to use broadcasts to send data to another activity.
In your Tab1Activity:
Intent dataIntent = new Intent();
dataIntent.setAction("com.your.app.DATA_BROADCAST");
dataIntent.putExtra("tag", "your data");
sendBroadcast(dataIntent);
Tab2Activity:
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String yourData = intent.getStringExtra("tag");
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("com.your.app.DATA_BROADCAST");
registerReceiver(receiver, filter);
You definitely want to reconsider using Activities as the content of your tabs. The more standard approach is to use one Activity that uses Tabs to only show part of the layout when a particular tab is selected.
The Android documentation has an excellent worked example, check out Hello, TabWidget.
Alternative
If for some reason you do need to use Activities, you can pass information between them by either adding values to the extras bundle within the Intent your using to open each Activity, or by extending the Application class.
By extending the Application class (and implementing it as a Singleton) you get an object that will exist whenever any of your application components exist, providing a centralized place to store and transfer complex object data between application components.
Also you can use static classes or SharedPreferences for data transfer between tabs.
the correct way is setting a static field into the activity that creates the tabs
public class greformanews extends TabActivity {
public static String JorgesysTitle;
...
...
...
so in your Activity defined in tab 1
#Override
protected void onPause() {
greformanews.JorgesysTitle = "JORGESYS =)";
super.onPause();
}
in your Activity defined in tab 2
//get value defined in Activity 1 !:)
String Title = greformanews.JorgesysTitle

How to setResult() for a TabActivity which contains activity for tabs

My TabActivity contains two tabs which calls two two different activities . I want to setResult() for the TabActivity when either one of the child finishes.
Is there any method to find out when my activity inside tab finishes ?
Thank you
-Eby
I found another way
` #Override
public void finishFromChild(Activity child)
{
setResult(REFRESH);
super.finishFromChild(child);
}
`
finsihFromChild will let us know when the child activity is finishing!!
#pentium10 thank you very much for your suggestion..
Ok i've got an even easier method to pass the result:
in your child activity do this
Intent list = this.getIntent();
list.setAction(Integer.toString(RESULT_CODE_TO_PASS));
finish();
and then in the parent do this:
#Override
public void finishFromChild(Activity child) {
Intent test = child.getIntent();
setResult(new Integer(test.getAction()));
super.finishFromChild(child);
}
You need to issue a Broadcast from your child activity(when finished) and implement the BroadcastReceiver on the class you want to catch the broadcast. You can use the extras to transfer data from one activity to another.

Sharing data between tabs

I am getting some glitches while making a tab enabled application.
I want to share data, between two tabs of my application.
How can I achieve the same?
Rgds
Robert
the correct way is setting a static field into the activity that creates the tabs
public class greformanews extends TabActivity {
public static String JorgesysTitle;
...
...
...
so in your Activity defined in tab 1
#Override
protected void onPause() {
greformanews.JorgesysTitle = "JORGESYS =)";
super.onPause();
}
in your Activity defined in tab 2
//get value defined in Activity 1 !:)
String Title = greformanews.JorgesysTitle
You need to use intents to different activities, or in that case of tabs.
Go to Android Common Tasks
And look at the subject below "some intent examples". This will get you started.
You basically need to put whatever values you want into a bundle and pass that over to the new activity using intent.putextras();

Categories

Resources