活动在调用startActivity时自行打开

时间:2019-03-20 12:49:36

标签: android android-fragments android-activity

我在此活动中有一个MainActivity,我得到了一个片段,该片段具有listView。单击此列表视图中的一个项目时,请执行以下操作:

startActivity(new Intent(v.getContext(), CategoryActivity.class));

这很好,因为它启动了我的CategoryActivity.class ,但是它首先再次启动了MainActivity,只有在打开我的CategoryActivity之后,它才启动。我不明白为什么会这样。因此,现在当我按下CategoryActivity中的后退按钮时,我进入MainActivity,可以再次按下后退按钮再次回到MainActivity

关于为什么会发生这种情况的任何想法?

修改:我的点击代码

storeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {

         // some irrelevant code

         final View v = view;

         final IQueryResult<IStoreItemConfiguration> itemConfigs = new IQueryResult<IStoreItemConfiguration>() {
             @Override
             public void result(List<IStoreItemConfiguration> results) {

                 // some code

                 startActivity(new Intent(v.getContext(), CategoryActivity.class));
             }
         }
    }
}

编辑:我的清单的相关部分

    <activity
        android:name=".activities.MainActivity"
        android:screenOrientation="portrait">
    </activity>
    <activity
        android:name=".activities.AuthenticationActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".activities.CategoryActivity"
        android:label="@string/title_activity_category"
        android:parentActivityName=".activities.MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme" />

2 个答案:

答案 0 :(得分:0)

startActivity(new Intent(getActivity(), CategoryActivity.class));

onBackPressed()中放置finish()finishAffinity(),如您所愿。

答案 1 :(得分:0)

Activity类调用Fragment的最佳方法是在Fragment中创建interface并在该接口中添加onItemClick()方法。现在,将其实施到您的第一个活动,然后从那里调用第二个活动。

如果要从片段中调用,可以使用此方法。

public class **YourFragmentClass** extends Fragment {

    Context context; //Declare the variable context

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {

    //Pass your layout xml to the inflater and assign it to rootView.
      View rootView = inflater.inflate(R.layout.**yourfragmentxml**, container, false); 
            context = rootView.getContext(); // Assign your rootView to context

            Button **yourButton** = (Button) rootView.findViewById(R.id.**your_button_id**);
            **yourButton**.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Pass the context and the Activity class you need to open from the Fragment Class, to the Intent
                    Intent intent = new Intent(context, **YourActivityClass**.class); 
                    startActivity(intent);
                }
            });
            return rootView;
        }
    }