The code below works, but the whole main view is replaced for the chart when I invoke itself.
I guess that the problem is located in how I start the chart Activity in startActivity(chart); in my framgment. If remove it, the fragment works well without replacement.
How could I send it to the main view???
public class Graph extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// LineGraph - class which draws the graph using achartengine
final LineGraph lineGraph = new LineGraph();
final Intent chart = lineGraph.getIntent(inflater.getContext());
startActivity(chart);
return inflater.inflate(R.layout.graph1, container, false);
}
}
Here the Activity:
public class MainCharts extends Activity {
Button btn1;
@TargetApi(11)
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dash_app);
btn1=(Button)findViewById(R.id.button1);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
StartFragment myFragment = new StartFragment();
ft.add(R.id.myFragment, myFragment);
ft.commit();
btn1.setOnClickListener(btnOnClickListener);
}
Button.OnClickListener btnOnClickListener = new Button.OnClickListener(){
@SuppressLint("NewApi")
@Override
public void onClick(View v){
Fragment newFragment;
newFragment = new Graph();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.myFragment, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
};
}
Take a look at this for an example on how to add an AChartEngine chart inside a fragment.