Java android onCreate 总是调用

标签 java android

当我锁定手机并在一个 Activity 中解锁时始终调用 onCreate 时,我遇到了问题。也许有人可以帮助我?这是我的onCreate,也许我做错了什么,调用了onCreate。当我第一次使用应用程序以及解锁手机时,创建时会在我的应用程序中调用,但是当我最小化我的应用程序并打开应用程序时,我的应用程序会调用 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());
                }
            }
        }
    }

list :

<?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>

最佳答案

我更改了我的 list :我添加了以下内容:android:configChanges="orientation|keyboardHidden|screenSize" 因为:

要声明您的 Activity 处理配置更改,请编辑 list 文件中的相应元素,以包含 android:configChanges 属性,该属性的值表示您要处理的配置。 android:configChanges 属性的文档中列出了可能的值(最常用的值是“orientation”,以防止在屏幕方向更改时重新启动,以及“keyboardHidden”以防止在键盘可用性更改时重新启动)。您可以在属性中声明多个配置值,用管道 | 分隔它们。性格。

关于Java android onCreate 总是调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41812945/

相关文章:

php - 将数据从服务器同步到 Android 设备的建议

java - Eclipse 为具有 'm' 后缀的域对象和类成员生成 getter/setter

java - 对位移位感到困惑(以字节为单位计算位数)

java - 我应该为 java 使用什么音频格式?

android - 如何在推送通知(Gcm)android 中显示图像?

Android Studio 注释格式

android - Ionic 5 Capacitor Android 不显示地理定位结果

android - 转换 GPS 坐标以匹配自定义 2d 户外布局图像

java - 当你需要使用 float 时如何真正避免浮点错误?

java - 在 Scala 中初始化 Apache Beam 测试管道失败