Android dynamic tab and multiple instance of an activity problem - android

I am making a chat application. Where i am using tabhost. I have a activity A to show the frndlist and activity B for chat window. Now when an entry in the frndlist clicked a tabwindow opens where i add tab dynamically using the intent of activity B. Now if there are more than one tab when i nevigate through those tab nothing get called(oncreate, onpause,onresume) and the containt remain same for all tabs. Only one instance of activity B is created.
Is there any idea to create chat application with tab(like yahoo mail chat).
thanks in advance
Please help
Rawcoder

Create this class too in your TabActivity
class PreExistingViewFactory implements TabContentFactory {
private final View preExisting;
protected PreExistingViewFactory(View view) {
preExisting = view;
}
public View createTabContent(String tag) {
return preExisting;
}
}
This works for me.

Related

Sharing data between two fragments

I'm currently trying to make a create account fragment and a choose profile pic fragment.
Fragment A/create account:
This houses Edittextfields for name, password, E-mail address.
There is also an Image view which represents the profile pic.
This Image view has a button on the side, which I want to use to switch over to Fragment B.
Fragment B/Choose Profile Pic:
Fragment is just an Image view and a couple of Image Buttons.
My Problem
Let's say the user has typed in all information on Fragment A and now wants to choose a profile pic, how do I save all the data temporary which is entered in Fragment A.
The second question is then, when the user clicks the Accept button on Fragment B, how do I sent the chosen Pic to Fragment A. So that I can switch over to Fragment A and restore the previously saved information and receive the drawable Image which the user has selected, to display it in the Image view.
Have a look at the new Android Architecture Components. Especially ViewModel.
https://developer.android.com/topic/libraries/architecture/viewmodel.html#sharing
There is a section on sharing data between fragments.
It's very common that two or more fragments in an activity need to
communicate with each other. Imagine a common case of master-detail
fragments, where you have a fragment in which the user selects an item
from a list and another fragment that displays the contents of the
selected item. This case is never trivial as both fragments need to
define some interface description, and the owner activity must bind
the two together. In addition, both fragments must handle the scenario
where the other fragment is not yet created or visible.
This common pain point can be addressed by using ViewModel objects.
These fragments can share a ViewModel using their activity scope to
handle this communication, as illustrated by the following sample
code:
public class SharedViewModel extends ViewModel {
private final MutableLiveData<Item> selected = new MutableLiveData<Item>();
public void select(Item item) {
selected.setValue(item);
}
public LiveData<Item> getSelected() {
return selected;
}
}
public class MasterFragment extends Fragment {
private SharedViewModel model;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
itemSelector.setOnClickListener(item -> {
model.select(item);
});
}
}
public class DetailFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
model.getSelected().observe(this, { item ->
// Update the UI.
});
}
}
Making Activity as an intermediate can enable your fragment communication.
Refer Communicating with Other Fragments for "fragment-activity-fragment" communication. If you want to persist the data refer SharedPreference of android.
Why you want to create 2 Fragments ?
You can achieve same thing with different views. Let's say you can create one Linear/Relative(Layout Group) Layout with all the controls of 1st Fragment and create another layout(Layout Group) with 2nd Fragment controls. And then using animation show and hide layouts so you can have both the things in same fragment or activity and user won't even notice that thing.
This way you don't have to save data temporary and you will have to implement all the things in single file.

Android: replacing a fragment from a button click within a fragment

I took over this android project from an outsourced developer. I do iOS development so I am still trying to get up to speed with android. The app has a top tab navigation which appears on all views. The home view is a fragment which has icons which when clicked on produce webviews that replace the home view, which the navigation tab still in view. I want to replace one of the webviews with a list view which is a fragment which I already have coded (EHallSchedFragment.java), so that when the icon is clicked on the list view replaces the home view with the navigation tab still in view. See the images below.
HOME VIEW
VIEW I WANT WHEN EXHIBIT HALL SCHEDULE ICON IS CLICKED ON
Here is the code for the onClick event for that button as it exists. It calls a webView:
ivExhall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openInternalWebview("http://www.web.org/m/content.aspx?id=7006");
//Need code here to open EhallSchedFragment.java
}
});
SO what I need is the onClick code that will open the fragment in the existing view, leaving the navigation tab in place.
Try this approach: Define an interface inside your fragment, implement it in your activity and then override the method there. Then finally, from your fragment, when a user clicks an item from a list, call the interface method to notify the activity of the event.
From Your Fragment
#Override
public void onClick()
{
//when a user selects an event from your list
switch(position)
{
((OnScheduleSelectedListener) getActivity()).switchFragment(new DetailsFragment());
}
}
public interface OnScheduleSelectedListener
{
void switchFragment(Fragment frag);
}
You can then do the following in your activity:
public class ScheduleAppActivity extends Activity implements OnScheduleSelectedListener
{
.............................
#Override
switchFragment(Fragment frag)
{
//check if fragment is in view here and if,
replaceFragment(fragment)
//else
addFragment(frag);
commit();
}
}
So at this point, the right fragment will be added to view.
I hope this helps you!

Clicking tab not showing Home activity

I have 3 tabs in my sample application with activity group. First tab contains search activity i.e.Home/Root activity and am displaying the results of search in another activity but under same tab i.e Tab1. When I press back button in result activity, it is going to search activity. Everything works fine till here. Now I want to go search activity by pressing tab1 instead of pressing back button. How can achieve this? I tried something like this
public class TabSample extends TabActivity {
public TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("OPT")
.setContent(new Intent(this, TabGroup1Activity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("EDIT")
.setContent(new Intent(this, TabGroup2Activity.class)));
tabHost.setCurrentTab(1);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String arg0) {
if (tabHost.getCurrentTabTag().equals("tab1")) {
//What should I do to display search activity here
} else {
tabHost.setCurrentTab(1);
}
}
});
tabHost.setFocusable(true);
tabHost.requestFocus();
}
}
Can anyone please help let me know how to invoke search activity when tab is pressed? What will go into if part? Because if I use tabHost.setCurrentTab(index), it will display result activity but not search activity.
NOTE: I followed the tutorial given in this link.
I think what you want to do is this: when the 'tab1' tag is selected, go back to TabGroup1Activity if (and only if) the current activity is not that activity (basically you want to simulate a 'back' press).
If so, what you want is this:
if (getCurrentActivity().getClass() != TabGroup1Activity.class)
getCurrentActivity().finish()
I'm not 100% sure I understand you fully, but let's see :)
In your onTabChanged listener you can switch on which tab have been tabed, and then open the activity as normal inside an activitygroup:
public void onTabChanged(String tabId) {
if (tabId.contentEquals("tab1")) {
Intent intent = new Intent(tabHost.getContext(), TabGroup1Activity.class);
View view = StartGroup.group.getLocalActivityManager().startActivity("tab1", intent).getDecorView();
StartGroup.group.setContentView(view);
}
}
I just reviewed my code and think there's a bit more to explain here. The problem is that you don't stack activities as normal. Instead the workaround is to make a content stack and change these instead. So what I have done is to create a class StartGroup which extends
ButtonHandlerActivityGroup:
public class StartGroup extends ButtonHandlerActivityGroup {
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static StartGroup group;
// Need to keep track of the history if you want the back-button to work properly,
// don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
// Start the root activity within the group and get its view
View view = getLocalActivityManager().startActivity("UserList", new Intent(this, UserList.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
replaceView(view);
}
public void back() {
if (history.size() > 0) {
// pop the last view
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
} else {
finish();
}
}
}
Then from the TabMaster class or what you call it you can use the StartGroup class to change the content view of an activity group.
This is something I wrote to work on devices from 2.2, so there might be an easier and more androidish way to accomplished it, but this works on almost all devices :)
Here is another thread where the use a similar approach:
Launching activities within a tab in Android
Let me know if I can help more.
There is an ArrayList in your ActivityGroup so override onPause() method in ActivityGroup and remove all the ids from ArrayList except the first one which must be your SearchActivity.
So when you go to other tab then comes back to SearchActivity( or on Tab1 ) Home will be displayed.

Android TabActivity Back Button Functionality with Multiple Child Activities

i have TabActivity in android project which contains some tabs. In each tab i can open various activities, and after open it in a tab i want go back to previous activity in same tab, but default android behavior close my root tab activity. How i can realise behavior that i need?
There are a few ways of doing this. The first involves creating a custom GroupActivity that will keep track of the stack from the LocalActivityManager and then extending that class for each of your tabs. For that, check out this tutorial:
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
A simpler approach is to keep an array of your tab's subviews within your initial ActivityGroup class and then override the back button. Here's some sample code:
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager()
.startActivity(id, newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
viewList.add(view); // Add id to keep track of stack.
this.setContentView(view);
}
public void previousView() {
if(viewList.size() > 0) {
viewList.remove(viewList.size()-1);
if (viewList.size() > 0)
setContentView(viewList.get(viewList.size()-1));
else
initView();
}else {
finish();
}
}
The initView() class holds all of the inflating of the original activity's view. This way, you can call this method to regenerate the original activity if there are no more views in the array.

Activity group problem in tab bar

I have used tabbar with activity group in my application. I have four tab like home, stock, citn, article. In my application first display home page from the home page user click in webview it will go to homepage1 activity. From home page1 activity user click stock tab it will go to stock activity. From the stock activity user click home tab it will go to homepage1 activity. I want to display home activity can any body tell how to do?
My question is switching between tab using activity group it will display last activity. I want to display first activity?
ok i will attach my code
spec = tabHost.newTabSpec("FirstGroup").setIndicator("FirstGroup",
getWallpaper()).setContent( new Intent(this,FirstGroup.class));
tabHost.addTab(spec);
View view =
getLocalActivityManager().startActivity("CitiesActivity",
new
Intent(this,CitiesActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
)).getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
run this example
http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity
switching between activity and tab
I have posted in pastebin, my link is
http://pastebin.com/1zG0HJgv
Hi Did u tried the tabchanged event as shown below
tabHost.addTab(tabHost.newTabSpec("tab1").setContent(
R.id.content_movies).setIndicator("",
getResources().getDrawable(R.drawable.icon)));
tabHost.addTab(tabHost.newTabSpec("tab2").setContent(
new Intent(this, Sample.class)).setIndicator("",
getResources().getDrawable(R.drawable.menu_icon)));
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
if(arg0.equals("tab1"))
{
/*write the code here to show the view
Currentclass, the class where you have used ontabchanged function and
Newclass is the class where you want to navigate*/
Intent obj_intent = new Intent(CureentClass.this,Newclass.class);
startActivity(obj_intent);
}
else if (arg0.equals("tab2")) {
// write the code here to show the view
}
//similarly for other tabs
});

Categories

Resources