Java android onCreate总是调用

时间:2017-01-23 18:14:47

标签: java android

当我锁定手机并在一个活动中解锁时总是调用onCreate,我遇到了问题。也许有人可以帮助我?这是我的onCreate,也许我做错了什么,叫做onCreate。在我的应用程序中创建是第一次在应用程序和我解锁手机时调用,但当我mminimalize我的应用程序并打开一个应用程序我的应用程序调用onResume

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

        MainActivity.logOut = true;
//        i = new Intent(getApplicationContext(), Sync.class);
//        startService(i);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        columns.add("nazwa");
        columns.add("start");
        sp = getSharedPreferences("pfref", Activity.MODE_PRIVATE);
        editor = sp.edit();

        placeholder = (TextView) findViewById(R.id.list_placeholder);
        buttonsHolder = new ArrayList<>();
        resultReceiver = new MyResultReceiver(new Handler());
        initializeView();
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        managerDB = ManagerDB.getInstance(getApplicationContext(), "name");
        progressBar = (ProgressBar) findViewById(R.id.pbWheel);
        tasks = new ArrayList<>();
        tasks.clear();
        btShowAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                routeDetailsShow();
            }
        });
        btNotDone = (Button) findViewById(R.id.all_items_not_done);
        btPhotoToSend = (Button) findViewById(R.id.bt_photo_to_send);
        btShowAllNotes = (Button) findViewById(R.id.bt_show_all_note);
        imgIsSend = (ImageView) findViewById(R.id.send);
        imgIsNet = (ImageView) findViewById(R.id.net);
        if (!MainActivity.xmlList.isEmpty()) {
            imgIsSend.setImageResource(R.drawable.red_arrow);
        } else {
            imgIsSend.setImageResource(R.drawable.green_arrow);
        }
        if (sp.getBoolean("serverOff", false) || sp.getBoolean("workOffLine", false)) {
            imgIsNet.setVisibility(View.VISIBLE);
        } else {
            imgIsNet.setVisibility(View.GONE);
        }
        btSort = (Button) this.findViewById(R.id.static_filter);
        btSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isSort)
                    showChangeLangDialog();
                else {
                    tasks.clear();
                    if (!isFiltr) {
                        getAllTasks();
                    } else {
                        getAllTasks2(column[0], sort[0], filtrText);
                    }
                    Page page = config.getItems().get(0).getPages().get(0);
                    myAdapter = new NewDataAdapter(MainActivity.this, parseWyborTrasyToDataRow(tasks), page);
                    lista.setAdapter(myAdapter);
                    isSort = false;
                    btSort.setTextColor(Color.BLACK);
                    btSort.setText("Sortowanie");
                }
            }
        });


        btFiltr = (Button) findViewById(R.id.static_filter2);
        btFiltr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isFiltr) {
                    showChangeLangDialog2();
                } else {
                    tasks.clear();
                    if (!isSort) {
                        getAllTasks();
                    } else {
                        getAllTasks1(sortColumn[0], sort[0]);
                    }
                    Page page = config.getItems().get(0).getPages().get(0);
                    myAdapter = new NewDataAdapter(MainActivity.this, parseWyborTrasyToDataRow(tasks), page);
                    lista.setAdapter(myAdapter);
                    isFiltr = false;
                    btFiltr.setTextColor(Color.BLACK);
                    btFiltr.setText("Filtrowanie");

                }
            }
        });

        btShowAllNotes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAllNotes();
            }
        });

        btShowAll.setEnabled(false);
        btNotDone.setEnabled(false);

        Gson gson = new Gson();
        List<File> productFromShared = new ArrayList<>();
        SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("TAG", Context.MODE_PRIVATE);
        String jsonPreferences = sharedPref.getString("TAG", "");

        Type type = new TypeToken<List<File>>() {
        }.getType();
        productFromShared = gson.fromJson(jsonPreferences, type);
        photoListSend = null;
        photoListSend = new ArrayList<>();
        if (productFromShared != null)
            photoListSend.addAll(productFromShared);

        List<String> productFromShared1 = new ArrayList<>();
        String jsonPreferences1 = sharedPref.getString("TAG1", "");

        Type type1 = new TypeToken<List<String>>() {
        }.getType();
        productFromShared1 = gson.fromJson(jsonPreferences1, type1);
        MainActivity.xmlList = null;
        MainActivity.xmlList = new ArrayList<>();
        if (productFromShared1 != null)
            xmlList.addAll(productFromShared1);


        btPhotoToSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isSend) {
                    photoList();
                } else {
                    Toast.makeText(MainActivity.this, "Trwa wysyłanie zdjęć ", Toast.LENGTH_LONG).show();
                }
            }
        });

        btNotDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notDone();
            }
        });
        lista.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                boolean enable = false;
                if (lista != null && lista.getChildCount() > 0) {
                    boolean firstItemVisible = lista.getFirstVisiblePosition() == 0;
                    boolean topOfFirstItemVisible = lista.getChildAt(0).getTop() == 0;
                    enable = firstItemVisible && topOfFirstItemVisible;
                }
                swipeRefreshLayout.setEnabled(enable);
            }
        });

        observer = new ContentObserver(new Handler(Looper.myLooper())) {
            public void onChange(boolean selfChange) {
                swipeRefreshLayout.setRefreshing(false);
                CheckerThread1 thread = new CheckerThread1(getApplicationContext());
                thread.execute();
                GPSTracker.getInstance(getApplicationContext());
            }
        };
        failureObserver = new ContentObserver(new Handler(Looper.myLooper())) {
            @Override
            public void onChange(boolean selfChange) {
                swipeRefreshLayout.setRefreshing(false);
            }
        };

        placeholder.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (NetworkUtils.isNetworkAvailable(getApplicationContext())) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        CheckerThread1 thread = new CheckerThread1(getApplicationContext());
                        thread.execute();
                        GPSTracker.getInstance(getApplicationContext());
                        isRefresh = true;
                        swipeRefreshLayout.setRefreshing(true);
                        showPlaceholder(myAdapter);
                    }
                } else {
                    swipeRefreshLayout.setRefreshing(false);
                    Toast.makeText(getApplicationContext(), "Nie ma dostępu do internetu", Toast.LENGTH_LONG).show();
                }
                return false;
            }
        });

//        placeholder.setOnTouchListener((v, event) -> {
//            if (NetworkUtils.isNetworkAvailable(getApplicationContext())) {
//                if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                    CheckerThread1 thread = new CheckerThread1(getApplicationContext());
//                    thread.execute();
//                    GPSTracker.getInstance(getApplicationContext());
//                    isRefresh = true;
//                    swipeRefreshLayout.setRefreshing(true);
//                }
//            } else {
//                swipeRefreshLayout.setRefreshing(false);
//                Toast.makeText(getApplicationContext(), "Nie ma dostępu do internetu", Toast.LENGTH_LONG).show();
//            }
//            return false;
//        });

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isSwipe = true;
                if (NetworkUtils.isNetworkAvailable(getApplicationContext())) {
                    CheckerThread thread = new CheckerThread(getApplicationContext());
                    thread.execute();
                    GPSTracker.getInstance(getApplicationContext());
                    isRefresh = true;
                    swipeRefreshLayout.setRefreshing(true);
                } else {
                    isRefresh = false;
                    swipeRefreshLayout.setRefreshing(false);
                    Toast.makeText(getApplicationContext(), "Nie ma dostępu do internetu", Toast.LENGTH_LONG).show();
                }
            }
        });
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
        if (LoginActivity.showProgresBar) {
//            LoginActivity.showProgresBar = true;
            CheckerThread thread = new CheckerThread(this);
            thread.execute();
            showProgresBar = false;
            GPSTracker.getInstance(this);
        } else {
            if (config != null) {
                gpsSwitchOnly = true;
                setCaption();
                refreshTables();
                if (!SyncConnection.running) {
                    fireUpSync(config.getSync());
                }
            }
        }
    }

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.eltegps022.smokkomunal">

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Activity.LoginActivity"
            android:windowSoftInputMode="stateVisible|adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity.AdminPanelActivity"
            android:windowSoftInputMode="stateVisible|adjustResize" />
        <activity
            android:name=".Activity.MainActivity"
            android:screenOrientation="landscape"
            android:windowSoftInputMode="stateHidden|adjustResize" />

        <service
            android:name=".sync.SyncOnceConnection"
            android:singleUser="true" />
        <service
            android:name=".sync.SyncOnceConnection"
            android:singleUser="true" />
        <service android:name=".photos.PhotoSender" />

        <activity
            android:name=".Activity.RouteDetailsActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.NotePGOActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.SignatureActivity"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.PhotoListActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.ConfirmPGOActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.DetailsPGOActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.ConfirmDetailsPGOActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.ExtraCargoActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.NoteToCargoActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />

        <service android:name=".Service.Sendrer" />
        <service android:name=".Service.SenderXML" />
        <service android:name=".Service.Sync" />


        <activity
            android:name=".Activity.RouteDetailsNotDoneActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.PhotoListToSendActivity"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.ShowAllNoteActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <activity
            android:name=".Activity.ShowAllNotesActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape" />
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我更改了我的清单:我添加了这个:android:configChanges="orientation|keyboardHidden|screenSize" 因为:

要声明您的活动处理配置更改,请编辑清单文件中的相应元素,以包含android:configChanges属性,其中包含表示要处理的配置的值。 android:configChanges属性的文档中列出了可能的值(最常用的值是“orientation”以防止在屏幕方向更改时重新启动,“keyboardHidden”以防止在键盘可用性更改时重新启动)。您可以通过使用管道分隔属性来声明属性中的多个配置值字符。