Where I bind to XML when extends from ActionBarActivity? - android

when I extends from Activity, I modify the fragment_main and find controls like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
MyButton = (Button) findViewById(R.id.button1);
}
but when I do this when extends from from ActionBarActivity gives me an error, so to do it work I have to comment:
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}*/
and set activity_main to fragment_main
setContentView(R.layout.fragment_main);
but I don't believe this be correct...

You should not get the button view on your activity, however it sides in a fragment. You can get it on your fragment such as PlaceholderFragment like you do on your activity. If needed, you should know something about how to interactive between Activity and Fragment. the link http://developer.android.com/guide/components/fragments.html may help you.

Related

Flow of execution for FragmentBasics at http://developer.android.com/training/basics/fragments/creating.html

Maybe I don't know the first thing about Android app development and xml (but I have 3 apps at Google Play Store), but what exactly determines what gets executed when?
To try to answer that question, I inserted Log statements in each method in each class file in FragmentBasics, which is the downloaded file from the URL in the Title.
Project structure:
I was surprised to see what the order of execution was.
MainActivity: `````onCreate
HeadlinesFragment: `````onAttach
HeadlinesFragment: `````onCreate
AbsListView: checkAbsListViewlLogProperty get invalid command
ArticleFragment: `````onCreateView
MainActivity: `````fragment_container IS null--two-pane mode
HeadlinesFragment: `````onStart
ArticleFragment: `````onStart
Why/how did onAttach and onCreate in HeadlinesFragment and onCreateView in ArticleFragment get executed before MainActivty "fragment container is Null..."?
And why does HeadlinesFragment not have onCreateView like ArticleFragment, but just onCreate?
I read that setContentView does this: "Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity." Since I was using a "large" device (tablet) and HeadlinesFragment and ArticleFragment are part of large\news_articles.xml, did that xml cause the classes to be instantiated?
I really feel like I know next to nothing about Android development. But I have those three apps ...
Here's the essence of the 3 classes mentioned:
public class MainActivity extends FragmentActivity
implements HeadlinesFragment.OnHeadlineSelectedListener
{
public void onCreate(Bundle savedInstanceState)
{
Log.w("MainActivity","`````onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles); // either normal or large
if (findViewById(R.id.fragment_container) == null)
Log.w("MainActivity","`````fragment_container IS null--two-pane mode");
else
{
Log.w("MainActivity","`````fragment_container not null--one-pane mode");
if (savedInstanceState != null)
return;
HeadlinesFragment firstFragment = new HeadlinesFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager() .beginTransaction()
.add(R.id.fragment_container, firstFragment)
.commit();
}
}
}
.
public class HeadlinesFragment extends ListFragment
{
public void onCreate(Bundle _savedInstanceState)
{
Log.w("HeadlinesFragment","`````onCreate");
super.onCreate(_savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
}
public void onStart()
{
Log.w("HeadlinesFragment","`````onStart");
super.onStart();
if (getFragmentManager().findFragmentById(R.id.article_fragment) != null)
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
public void onAttach(Activity _activity)
{
Log.w("HeadlinesFragment", "`````onAttach");
super.onAttach(_activity);
}
}
.
public class ArticleFragment extends Fragment
{
public View onCreateView(LayoutInflater _inflater, ViewGroup _container, Bundle _savedInstanceState)
{
Log.w("ArticleFragment", "`````onCreateView ");
View v = _inflater.inflate(R.layout.article_view, _container, false);
return v;
}
public void onStart()
{
Log.w("ArticleFragment","`````onStart");
super.onStart();
}
}
And the essence of the .xml:
article_view.xml
<TextView android:id="#+id/article" />
news_articles.xml
<FrameLayout android:id="#+id/fragment_container" />
news_articles(large).xml
<LinearLayout>
<fragment
android:name="com.example.android.fragments.HeadlinesFragment"
android:id="#+id/headlines_fragment">
<fragment
android:name="com.example.android.fragments.ArticleFragment"
android:id="#+id/article_fragment"/>
</LinearLayout> [1]: http://i.stack.imgur.com/JPCZu.png
Someone else already did the same as you, but built also a nice image with how in practice the lifecycle happens. Here is a direct reference to the image diagram.

Android Activity, extend BaseActivity

What would be the correct way of implementing my idea? Should I use inflate?
Want to make aprox. 100 activities. All of them extending the same base-class (BaseActivity). What I want to accomplish is a BaseActivity that will show 3 View's (LeftView, MainView, RightView) in a LinearLayout (horizontal). This is not the problem. MainView is empty.
The problem arises when I want to design MyActivity (extends BaseActivity). My idea was that the R.layout designed in MyActivity ONLY was shown in MainView (part of BaseActivity).
Is this possible? and what would be the smartes/best way to implement this?
Kind regards, Ole
OK, the way I choose to handle this, was by inflating the R.layout:
public class BaseActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
return;
}
}
public class MyActivity extends BaseActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout da_layout_main = (RelativeLayout) findViewById( R.id.da_layout_main);
View view_child = getLayoutInflater().inflate( R.layout.my_activity, null);
da_layout_main.addView( view_child);
}
}

Fragment placed on top of NavigationDrawer

I'm trying to add a PreferenceFragmentin my application. The problem is, it's auto placed on top of my NavigationDrawer.
public class SetPreferenceActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
navigationDrawer(); // Loads the NavigationDrawer
getFragmentManager().beginTransaction().replace(android.R.id.content,
new Settings()).commit();
}
As you can see I load the "SettingsFragment" and replace the content with it? (I'm unsure) but it places it on top of everything else.. Here's my Settings fragment.
public class Settings extends PreferenceFragment {
static final String TAG = "MAIN";
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
Everything work as expected, BUT the PreferenceFragment are loaded in front, covering up the NavigationDrawer slideout, I tried calling bringToFront(); on the listview, with no luck.
A picture for reference :
Is it possible to tell the fragment to load behind the listview? I also tought about loading the fragment in a ViewPager, but I get an error that the Pager Adapter wont accept fragments of type PreferenceFragment.
Don't replace android.R.id.content, use the the id of the FrameLayout you have in the layout that contains your DrawerLayout.
In addition to what adneal said (in case others have the same problem):
The Activity which calls the PreferenceFragment needs to have the setContentView() method if you extend the Activity with your NavigationDrawer:
public class SetPreferenceActivity extends MyNavigationDrawer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
getFragmentManager().beginTransaction().replace(R.id.drawer_frame_layout,
new Settings()).commit();
}
And the settings.xml should only contain a FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I had same issue and i resolved it by replacing android.R.id.content to R.id.container.

Trying to change TextView String

I´ve been looking for answers, and have tried a lot of things, but nothing is working...
So, Im doing what I think is a simple app (I just started learning)
So I have an activity sending a couple of string to another where a couple of TextViews should chnage ther values to the new String, but this is not happening. Here you have the code:
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Intent intent = getIntent();
String mail = intent.getStringExtra(MainActivity.MAIL);
String password = intent.getStringExtra(MainActivity.PASSWORD);
setContentView(R.layout.fragment_login);
TextView displayMailSetter = (TextView) findViewById(R.id.mailView);
displayMailSetter.setText(mail);
displayMailSetter.postInvalidate();
TextView displayPasswordSetter = (TextView)findViewById(R.id.passwordView);
displayPasswordSetter.setText(password);
displayPasswordSetter.postInvalidate();
displayMailSetter.postInvalidate();
setContentView(R.layout.activity_login);
}
}
Try taking out these lines of code:
displayMailSetter.postInvalidate();
// Don't take out your displayPasswordSetter initilization and setText
displayPasswordSetter.postInvalidate();
displayMailSetter.postInvalidate();
setContentView(R.layout.activity_login);
Change these lines of code:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
to
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
and take out the origional (right under the intents).
setContentView(R.layout.fragment_login);
The problem was that you were setting the layout to 3 different layouts. The first and third were the same layout, but didn't have the TextViews. The first and third are unnessecary, you should only set the layout once. Once you set it to fragment_login, the layout had the TextViews, set the text of them, but the layout was then changed to a layout without the TextViews.

FragmentActivity not calling ListFragment

I am trying to implement a ListFragment. The idea is to use CursorLoader. The code for FragmentActivity is
public class XYZ extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
XYZFragment list = new XYZFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
Now I assume that onActivityCreate of XYZFragment should be called but that is not happening instead I am getting a the below image on the Emulator. I am looking for an explanation as to what is happening and what I am doing wrong?
Thanks.
Change onActivityCreate to onActivityCreated and you should get the expected results.

Categories

Resources