0

I have a main activity extending ActionBarActivity. In my activity i create a fragment which isn't showing up. I modified the main activity to extend FragmentActivity and in this case the fragment is showing up. Why isn't the fragment showing up when my activity extends ActionBarActivity? I know that ActionBarActivity extends FragmentActivity so in theory ActionBarActivity should support fragments.

Edit: I resolved it. I wouldn't set any content view on my activity. Now that i set it it works when i extend ActionBarActivity. Still is weird that even if i don't set it, when i extend FragmentActivity it works.

This is my main activity code:

package com.example.yamba;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
//import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class StatusActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState == null)
    {
        StatusFragment fragment = new StatusFragment();

        FragmentTransaction fr = getSupportFragmentManager().beginTransaction();
        fr.add(android.R.id.content, fragment);
        fr.commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.status, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

This is my fragment code:

package com.example.yamba;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.os.AsyncTask;
import android.text.TextWatcher;
import android.text.Editable;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;

import com.marakana.android.yamba.clientlib.YambaClient;
import com.marakana.android.yamba.clientlib.YambaClientException;

public class StatusFragment extends Fragment implements OnClickListener
{
private static final String TAG = "StatusFragment";
private EditText editStatus;
private Button buttonTweet;
private TextView textCount;
private int defaultColor;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.activity_fragment, container, false);

    editStatus = (EditText) view.findViewById (R.id.editStatus);
    buttonTweet = (Button) view.findViewById (R.id.buttonTweet);
    textCount = (TextView) view.findViewById (R.id.textCount);

    buttonTweet.setOnClickListener(this);
    defaultColor = textCount.getTextColors().getDefaultColor();
    editStatus.addTextChangedListener(new TextWatcher()
    {
        public void afterTextChanged (Editable s)
        {
            int count = 140 - editStatus.length();
            textCount.setText(Integer.toString(count));
            textCount.setTextColor(Color.GREEN);

            if(count<20  && count >= 10)
                textCount.setTextColor(Color.YELLOW);
            else if (count<10)
                textCount.setTextColor(Color.RED);
            else
                textCount.setTextColor(defaultColor);
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after){}

        public void onTextChanged (CharSequence s, int start, int b, int c){}
    });

    return view;
}

public void onClick(View view)
{
    String status = editStatus.getText().toString();
    Log.d(TAG, "onClicked with status: " + status);

    new PostTask().execute(status);
    editStatus.getText().clear();
}

private final class PostTask extends AsyncTask<String, Void, String>
{
    protected String doInBackground(String... params)
    {
        YambaClient yambaCloud = new YambaClient("student", "password");

        try
        {
            yambaCloud.postStatus(params[0]);
            return "Succesfuly posted!";
        }
        catch (YambaClientException e)
        {
            e.printStackTrace();
            return "Failed to post to yamba sevice!";
        }
    }

    @Override
    protected void onPostExecute (String result)
    {
        super.onPostExecute(result);

        Toast.makeText(StatusFragment.this.getActivity(), result, Toast.LENGTH_LONG).show();
    }
}
}

2 Answers2

0

You are calling the FragmentManager method add into the wrong container. The id you provide must reference to a View (usually a layout) that is part of your activity's layout. So android.R.id.content is surely wrong, look into your activity xml layout and find the correct id (maybe is R.id.content (without android) ? )

rickyalbert
  • 2,552
  • 4
  • 21
  • 31
  • Here there is an example of how to do it: http://stackoverflow.com/questions/25577960/cant-add-a-fragment-when-creating-a-new-activity – rickyalbert Oct 29 '14 at 15:19
  • I don't really know what you are talking about because i'm just learning... but it works as it is (android.R.id.content) if my StatusActivity extends FragmentActivity (and not ActionBarActivity). – Costin Victor Oct 29 '14 at 15:42
  • ActionBarActivity is a subclass of FragmentActivity so it should work with that too – rickyalbert Oct 29 '14 at 17:06
0

It appears to be a bug. As a workaround you could try to add fragment inside a post command. It did the trick for me.

new Handler().post(new Runnable() {
    @Override
    public void run() {
        StatusFragment fragment = new StatusFragment();

        FragmentTransaction fr = getSupportFragmentManager().beginTransaction();
        fr.add(android.R.id.content, fragment);
        fr.commit();
    }
});
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86