使用MainActivity中的2个片段加载SecondActivity

时间:2014-12-27 08:16:40

标签: android android-fragments

我正在尝试加载包含来自主要活动的两个片段的第二个活动 我有一个包含两个片段的活动。

第二个活动布局如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity"
    tools:ignore="MergeRootFrame" >

    <fragment android:name=".SecondActivity$MainFragment"
        android:id="@+id/mainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/googleAdFragment"
        tools:layout="@layout/second_fragment" />

    <fragment android:name=".MainActivity$GoogleAdFragment"
        android:id="@+id/googleAdFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        tools:layout="@layout/google_ad_fragment"/>

</RelativeLayout>

使用MainActivity中的以下代码加载第二个活动

public void btnShowFDIntCal(View view) {
    // Load result 
    //Intent intent = new Intent(this, activity_result.class);
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);
}

第二项活动

package xxxx.test;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;


public class SecondActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fdintcalc_activity_layout);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new MainFragment())
                    .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.menu_fdint_calc, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public static class MainFragment extends Fragment {

        public MainFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fdintcalc_fragment, container, false);
            return rootView;
        }
    }
}

我的问题是第二个活动在没有googleAdFragment的情况下加载。

有人可以帮助我吗?

0 个答案:

没有答案