I created a Tabbed Activaity using Android Studio 1.2.2 wizard, I hope to close this window when I click the button btnClose, I write the code rootView.getContext().finish(); , but it's wrong,
and PlaceholderFragmentOld.this.finish(); is wrong too.
How can I do? Thanks!
PlaceholderFragmentOld.java
package ui.tab;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.widget.Button;
import com.google.android.gms.ads.AdView;
import info.dodata.messagecleanup.R;
import bll.PublicParFun;
import info.dodata.messagecleanup.R;
public class PlaceholderFragmentOld extends Fragment {
private AdView adView;
public PlaceholderFragmentOld() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.cleanup_delete_fragment_old, container, false);
adView=(AdView) rootView.findViewById(R.id.adView);
PublicParFun.SetAD(adView);
SetButtons(rootView);
return rootView;
}
private void SetButtons(final View rootView){
rootView.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rootView.getContext().finish();
}
});
}
}
CleanupDelete.java
package ui;
import java.util.Locale;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import info.dodata.messagecleanup.R;
import ui.tab.PlaceholderFragmentOld;
import ui.tab.PlaceholderFragmentPerson;
import ui.tab.PlaceholderFragmentReduce;
import ui.tab.PlaceholderFragmentTrim;
public class CleanupDelete extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cleanup_delete);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
switch(position){
case 0:
fragment = new PlaceholderFragmentOld();
break;
case 1:
fragment = new PlaceholderFragmentReduce();
break;
case 2:
fragment = new PlaceholderFragmentTrim();
break;
case 3:
fragment = new PlaceholderFragmentPerson();
break;
default:
throw new IllegalArgumentException("Invalid section number");
}
return fragment;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.Tab_Old);
case 1:
return getString(R.string.Tab_Reduce);
case 2:
return getString(R.string.Tab_Trim);
case 3:
return getString(R.string.Tab_Person);
}
return null;
}
}
}
cleanup_delete.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/pager"
android:layout_width="match_parent" android:layout_height="match_parent"
/>
cleanup_delete_fragment_old.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border_ui" >
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingTop="20dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:orientation="vertical" >
<TextView
android:id="#+id/tVDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
style="#style/myTextDesciption"
android:text="This is a descript"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutToolBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="4" >
<Button
android:id="#+id/btnDelete"
style="#style/myTextMedium"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:text="#string/BtnDelete" />
<Button
android:id="#+id/btnClose"
style="#style/myTextMedium"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:text="#string/BtnClose" />
</LinearLayout>
</RelativeLayout>
finish() can only be called in an Activity. Given that, you do it this way:
// Somewhere in your fragment
private void finishActivity() {
if(getActivity() != null) {
getActivity().finish();
}
}
And you could then call that method to finish your Activity:
// Somewhere in your click listener
finishActivity();
IMPORTANT: There is a possibility for getActivity() to return null. This is especially dangerous when the fragment is being detached from your Activity (when using a ViewPager or when you're juggling between numerous fragments in an activity). Calling it without checking will pose your code to the threat of NullPointerException.
You may do something like this.
#Override
public void onClick(View v) {
getActivity().finish();
}
You should use getActivity().finish();
In order to move from a fragment to activity using Kotlin, add the code below:
if(activity != null) {
activity?.finish()
}
Related
I have a fragment(A) , where another fragment(B) is being opened . When I press back button it just refreshes the fragment(B) instead of exiting from it and returning to fragment A.
I tried poping back the fragment A in the Activity's onBackPressed callback method but it didn't change a thing :
#Override
public void onBackPressed() {
if(B.active)
{
mFragmentManager.popBackStack( A.TAG , 0);
B.active = false;
}
}
** the active boolean is just something I added as part of the solution. It's initialized to TRUE once the fragment is instantiated.
I don't know how to commit a project!
package com.example.a;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/9.
*/
public class ActivityA extends AppCompatActivity{
#InjectView(R.id.c)
LinearLayout c;
#InjectView(R.id.btn)
TextView btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
ButterKnife.inject(ActivityA.this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.e, FragA.newInstance(), "A");
// fragmentTransaction.addToBackStack("A");
fragmentTransaction.commitAllowingStateLoss();
}
});
}
}
package com.example.a;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/10.
*/
public class FragA extends Fragment{
private View rootView;
public static FragA newInstance() {
return new FragA();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public FragA() {
}
#InjectView(R.id.a)
LinearLayout a;
#InjectView(R.id.btn)
TextView btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fraga,container,false);
ButterKnife.inject(this,rootView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.e, FragB.newInstance(), "B");
fragmentTransaction.addToBackStack("B");
fragmentTransaction.commitAllowingStateLoss();
}
});
return rootView;
}
}
package com.example.a;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/10.
*/
public class FragB extends Fragment{
private View rootView;
public static FragB newInstance() {
return new FragB();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public FragB() {
}
#InjectView(R.id.b)
LinearLayout b;
#InjectView(R.id.btn)
TextView btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragb,container,false);
ButterKnife.inject(this, rootView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().popBackStackImmediate();
}
});
return rootView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/c"
android:background="#4b14b1"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/e"
android:layout_width="match_parent"
android:layout_height="300dp">
</FrameLayout>
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="c"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/a"
android:background="#4b14b1"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="A"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/b"
android:background="#4b14b1"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="B"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I'm constantly receiving data in MainActivity and I need it to only be written a textView when the fragment that contains the textView is inflated. Obviously the app crashes if I try to write to a textView that is in a fragment currently not in display. I have made a dummy activity to explain my problem and I'm using a countdown timer to represent the data that is continually changing in the MainActivity. The issue resides in MainActivity.
Would someone mind explaining how I can only write to the textView when it is inflated?
Thanks
FragmentA.java (Fragment B and C are almost identical)
package com.felhr.scrolltabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Luke on 20/04/2015.
*/
public class FragmentA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
MainActivity.java
package com.felhr.scrolltabs;
import android.os.Bundle;
import android.os.CountDownTimer;
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.widget.TextView;
public class MainActivity extends FragmentActivity {
ViewPager viewPager = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
final TextView timer = (TextView)findViewById(R.id.textViewTimer);
// Countdown timer code -> to be run constantly but only displayed in Fragment A is in focus
// This section breaks the code
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
timer.setText("done!");
}
}.start();
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = null;
Log.d("SWIPE","get Item is called"+i);
if (i==0)
{
fragment=new FragmentA();
}
if (i==1)
{
fragment=new FragmentB();
}
if (i==2)
{
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
Log.d("SWIPE","get count is called");
return 3;
}
// talks to title to give the page
#Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "Tab 1";
}
if (position == 1) {
return "Tab 2";
}
if (position == 2) {
return "Tab 3";
}
return null;
}
}
fragment_a.xml (fragments b and c are almost identical)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C3f3f3">
<TextView
android:id="#+id/textViewTimer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment A"
android:textColor="#000000"
android:gravity="center"/>
</RelativeLayout>
activity_main.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pager">
<android.support.v4.view.PagerTitleStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/title"
android:background="#33B5E5">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
I have created a fragment activity with 3 tabs. When I move from tab 1 to tab 3, it takes around 5 seconds to load tab 3( due to some time consuming task ). During this loading time, I want to show a dialog.
I put the dialog inside onTabSelected(), but the dialog shows up only after the 3rd tab display comes up. I want it to show between "the time I clicked the 3rd tab" and "the time the 3rd tab actually comes up". Please do let me know ur suggestions.
Editted :
Sample code :
MainActivity.java:
package com.example.trial;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM ="selected_navigation_item";
ProgressDialog nDialog;
Fragment dashboardFrag = new DashboardTab();
Fragment Tab1 = new Tab1();
Fragment Tab2 = new Tab2();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Tab1")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab2")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab3")
.setTabListener(this));
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
if (tab.getPosition() == 0) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, Tab1).commit();
}
else if (tab.getPosition() == 1) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, Tab2).commit();
}
else if (tab.getPosition() == 2) {
showProgress1();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, dashboardFrag).commit();
}
}
void showProgress1(){
nDialog = new ProgressDialog(MainActivity.this); //Here I get an error: The constructor ProgressDialog(PFragment) is undefined
nDialog.setMessage("Loading..");
nDialog.setTitle("Fetching Data");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
}
Tab1.java
package com.example.trial;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
return rootView;
}
}
Tab2.java
package com.example.trial;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2, container, false);
return rootView;
}
}
DashboardTab.java
package com.example.trial;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class DashboardTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dashboard, container, false);
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rootView;
}
}
activity_main.xml
<FrameLayout android:id="#+id/container" android:layout_height="match_parent" android:layout_width="match_parent" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
</FrameLayout>
tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dashBoard"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
I didn't find something like you are telling, may be you are having issues with your phone memory, however an approach to show dialog when performing time consuming tasks through your code
public class DashboardTab extends Fragment {
private ProgressDialog nDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dashboard, container, false);
showProgress1();
YourTimeConsumingTask();
nDialog.dismiss();
return rootView;
}
private void YourTimeConsumingTask(){
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void showProgress1() {
nDialog = new ProgressDialog(getActivity()); // Here I get an error:
// The constructor
// ProgressDialog(PFragment)
// is undefined
nDialog.setMessage("Loading..");
nDialog.setTitle("Fetching Data");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
}
This question has been asked before, however, I have not found an appropriate answer for my problem. I am building a simple fragment activity that displays three buttons at the top of the screen in one container that when pressed update the fragment in the rest of the screen. Just like a tab host. I am using android.support.v4.app.Fragment in all code, but still cannot get the transaction manager to display the fragment in the container. I've been banging my head on the keyboard for hours and connot find the problem.
Here is the main activity extending FragmentActivity:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class TestUDPFragmentActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_udpfragment);
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.menu_container, new MenuFragment()).commit();
}
}
}
Here is the MenuFragment class extending fragment for my tabs:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.*;
public class MenuFragment extends Fragment{
Fragment frag;
FragmentTransaction fragTransaction;
public MenuFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frament_menu, container, false);
frag = new StringsFragment();
fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);
System.out.println(fragTransaction.toString());
fragTransaction.commit();
Button btnString = (Button) view.findViewById(R.id.button_string);
Button btnSlider = (Button) view.findViewById(R.id.button_slider);
Button btnTouch = (Button) view.findViewById(R.id.button_touch);
btnString.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new StringsFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
btnSlider.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new SlidersFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
btnTouch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new TouchFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
return view;
}
}
Here is one of the three fragments that I can't get to display:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.*;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class StringsFragment extends Fragment {
public StringsFragment() {
}
public View onCreatView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_strings, null);
return rootView;
}
}
activity_test_udpfragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/menu_container"
android:background="#eee"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="#+id/container"
android:background="#aaa"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Here is frament_menu:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="android.att/buttonBarStyle" >``
<Button
android:id="#+id/button_string"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="String" />
<Button
android:id="#+id/button_slider"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Slider" />
<Button
android:id="#+id/button_touch"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Touch" />
</LinearLayout>
And here is fragment_strings:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:scaleType = "centerCrop"
android:src="#drawable/me_hooded" />
</RelativeLayout>
Any help is greatly appreciated!
EDIT:
Here is new TestUDPActivityFragment with buttons to change fragment:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Bundle;
public class TestUDPFragmentActivity extends FragmentActivity {
Fragment frag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_udpfragment);
Button btnString = (Button)findViewById(R.id.btn_string);
Button btnSlider = (Button)findViewById(R.id.btn_slider);
Button btnTouch = (Button)findViewById(R.id.btn_touch);
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new StringsFragment()).commit();
btnString.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new StringsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
btnSlider.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new SlidersFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
btnTouch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new TouchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
}
}
}
I can see a couple of problems...
First, your FrameLayout containers are defined in your activity_test_udpfragment.xml layout file which is inflated by your TestUDPFragmentActivity. The problem with attempting to create / add your StringsFragment in your MenuFragment code using the following...
fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);
...is the MenuFragment inflates a layout from your frament_menu.xml which doesn't contain a FrameLayout with id of R.id.container.
That's your first problem but the second is with attempting to use a Fragment to create / add a Fragment in the first place. Your MenuFragment is not able to use the Framelayout in the TestUDPFragmentActivity.
In short, I suspect you want both Fragments to appear in your Activity layout (and not have the StringsFragment as a 'child' of MenuFragment) in which case you need to put all code for creating / adding both Fragments into your Activity.
I need to setText in TextView inside the Fragment from FragmentActivity.
((TextView) findViewById(R.id.textview_in_fragment)).setText("Text")
in FragmentActivity doesnt works. I found out how to setText in Fragment, but I need to setText in Fragment layout from FragmentActivity. I searched a lot, but I couldnt find a direct answer.
you can try to code below,
just create and method in your fragmet to set the value of it
public void setText(String text)
{
button.setText(text);
}
BasicFragment
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class BasicFragment extends Fragment {
Button button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_basic, container, false);
button = (Button) view.findViewById(R.id.fragment_button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity,
"toast_you_just_clicked_a_fragment",
Toast.LENGTH_LONG).show();
}
}
});
return view;
}
public void setText(String text ){
button.setText(text);
}
}
BasicFragmentActivity
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.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BasicFragmentActivity extends FragmentActivity {
BasicFragment b;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
b.setText("test");
}
});
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_content);
if (fragment == null) {
b = new BasicFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, b);
ft.commit();
}
}
}
activity_fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_in_fragmentactivity" />
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
fragment_button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#ffffaa">
<Button
android:id="#+id/fragment_button"
android:text="BtnFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>