Android主要活动太慢了

时间:2016-10-18 14:05:24

标签: android android-fragments

我的主要活动上有9个按钮,可以在点击时导航到特定的片段。但它主要线程重。如何减少代码大小。我可以使用开关而不是定义9个按钮 这是主要的活动代码

MainActivity

package com.gowarbaam.baluchistannationalparty;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.gowarbaam.baluchistannationalparty.Fragments.AboutApp;
import com.gowarbaam.baluchistannationalparty.Fragments.AboutParty;
import com.gowarbaam.baluchistannationalparty.Fragments.Aims;
import com.gowarbaam.baluchistannationalparty.Fragments.CityList;
import com.gowarbaam.baluchistannationalparty.Fragments.Events;
import com.gowarbaam.baluchistannationalparty.Fragments.History;
import com.gowarbaam.baluchistannationalparty.Fragments.Martyrs;
import com.gowarbaam.baluchistannationalparty.Fragments.TwitterMain;

public class MainActivity extends AppCompatActivity {

    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();





            final Button HistoryBtn = (Button) findViewById(R.id.historyBtn);
        HistoryBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView, new History()).commit();

            }
        });

        final Button AimsBtn = (Button) findViewById(R.id.aimsBtn);
        AimsBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView, new Aims()).commit();

            }
        });

        final Button MembersBtn = (Button) findViewById(R.id.membersBtn);
        MembersBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView, new CityList()).commit();

            }
        });

        final Button PhotoBtn = (Button) findViewById(R.id.martyrsBtn);
        PhotoBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView, new Martyrs()).commit();

            }
        });

        final Button TweetBtn = (Button) findViewById(R.id.tweetsBtn);
        TweetBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView, new TwitterMain()).commit();

            }
        });

        final Button AboutParty = (Button) findViewById(R.id.aboutParty);
        AboutParty.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView, new AboutParty()).commit();

            }
        });

        final Button b = (Button) findViewById(R.id.eventsBtn);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView, new Events()).commit();

            }
        });

        final Button AboutApp = (Button) findViewById(R.id.aboutApp);
        AboutApp.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView, new AboutApp()).commit();

            }
        });

}

@Override
public void onBackPressed() {

    Intent myIntent = new Intent(this, MainActivity.class);
    this.startActivity(myIntent);

}

}

2 个答案:

答案 0 :(得分:1)

您的应用程序可能变慢的一个原因是您对onBackPressed()事件的处理方式。每次在应用程序的任何位置点击后退按钮时,您都会创建一个新的MainActivity,但是您从未完成上一个,因为这是您的MainActivity,您几乎可以在每次背压时重新创建整个应用程序。

答案 1 :(得分:0)

您的问题似乎不是由于您的活动类,而是您的XML文件。

如果有的话,我建议为每个片段创建一个活动,只是为了测试。你可以看到下面的内容。

<强> AboutAppActivity

package com.gowarbaam.baluchistannationalparty;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.gowarbaam.baluchistannationalparty.Fragments.AboutApp;

public class AboutAppActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.containerView, new AboutApp())
                .commit();
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    // This is not for performance, just DRY
    private View.OnClickListener btnOnClickListener(final Class activityClass) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), activityClass);
                startActivity(i);
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (...)
        final Button AboutApp = (Button) findViewById(R.id.aboutApp);
        AboutApp.setOnClickListener(this.btnOnClickListener(AboutAppActivity.class));
    }


}