Skip to content

Commit aeb3633

Browse files
committed
ADD Fragment usage documentation
1 parent 75de86e commit aeb3633

File tree

12 files changed

+708
-22
lines changed

12 files changed

+708
-22
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ Usage
7676

7777
* [Properties](#Properties)
7878
* [Exit Codes](#Exit-Codes)
79-
* [Example Kotlin](#Example-Kotlin)
80-
* [Example Java](#Example-Java)
79+
* [Example Activity (Kotlin and Java)](#Example-Activity-(Kotlin-and-Java))
80+
* [Example Fragment (Java and Kotlin)](#Example-Fragment-(Kotlin-and-Java))
8181

8282
### Properties
8383

@@ -248,7 +248,9 @@ They can be calles using ```OnCompleteListener.$NAME$```
248248
| 12 | ```EXIT_CODE_ERROR_STORAGE_PERMISSONS_NOT_GRANTED``` | Storage permissions not granted for custom dialog |
249249
| 13 | ```EXIT_CODE_ERROR_WRONG_DECRYPTION_PASSWORD``` | Cannot decrypt provided backup file because the password is incorrect |
250250

251-
### Example Kotlin
251+
### Example Activity (Kotlin and Java)
252+
253+
#### Kotlin
252254

253255
* ##### Backup
254256

@@ -291,7 +293,7 @@ They can be calles using ```OnCompleteListener.$NAME$```
291293
.restore()
292294
```
293295

294-
### Example Java
296+
#### Java
295297

296298
* ##### Backup
297299

@@ -326,10 +328,24 @@ They can be calles using ```OnCompleteListener.$NAME$```
326328
roomBackup.restore();
327329
```
328330

331+
### Example Fragment (Kotlin and Java)
332+
333+
##### Kotlin
334+
335+
[`FragmentActivity.kt`](app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/FragmentActivity.kt)
336+
[`MainFragment.kt`](app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/MainFragment.kt)
337+
338+
##### Java
339+
340+
[`FragmentActivityJava.java`](app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/FragmentActivityJava.java)
341+
[`MainFragmentJava.java`](app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/MainFragmentJava.java)
342+
343+
329344
Sample app
330345
----------
346+
331347
1. Download this repo
332-
2. Unzip
348+
2. Unzip
333349
3. Android Studio --> File --> Open --> select this Project
334350
4. within the app folder you find the sample app
335351

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies {
5353
implementation 'androidx.appcompat:appcompat:1.4.0'
5454
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
5555
implementation project(path: ':core')
56+
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
5657
testImplementation 'junit:junit:4.13.2'
5758
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
5859
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
12-
<activity android:name=".MainActivity"
12+
<activity
13+
android:name=".FragmentActivityJava"
14+
android:exported="false" />
15+
<activity
16+
android:name=".FragmentActivity"
17+
android:exported="false" />
18+
<activity
19+
android:name=".MainActivity"
1320
android:exported="true">
1421
<intent-filter>
1522
<action android:name="android.intent.action.MAIN" />
@@ -21,4 +28,4 @@
2128
<activity android:name=".MainActivityJava" />
2229
</application>
2330

24-
</manifest>
31+
</manifest>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package de.raphaelebner.roomdatabasebackup.sample
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import de.raphaelebner.roomdatabasebackup.core.RoomBackup
6+
7+
/**
8+
* MIT License
9+
* <p>
10+
* Copyright (c) 2021 Raphael Ebner
11+
* <p>
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
* <p>
19+
* The above copyright notice and this permission notice shall be included in all
20+
* copies or substantial portions of the Software.
21+
* <p>
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
* SOFTWARE.
29+
*/
30+
class FragmentActivity : AppCompatActivity() {
31+
32+
lateinit var backup: RoomBackup
33+
34+
override fun onCreate(savedInstanceState: Bundle?) {
35+
super.onCreate(savedInstanceState)
36+
setContentView(R.layout.activity_fragment)
37+
38+
backup = RoomBackup(this)
39+
40+
val transaction = supportFragmentManager.beginTransaction()
41+
transaction.replace(R.id.fragment_container_view, MainFragment())
42+
transaction.commit()
43+
}
44+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package de.raphaelebner.roomdatabasebackup.sample;
2+
3+
import android.os.Bundle;
4+
5+
import androidx.appcompat.app.AppCompatActivity;
6+
import androidx.fragment.app.FragmentTransaction;
7+
8+
import de.raphaelebner.roomdatabasebackup.core.RoomBackup;
9+
10+
/**
11+
* MIT License
12+
* <p>
13+
* Copyright (c) 2021 Raphael Ebner
14+
* <p>
15+
* Permission is hereby granted, free of charge, to any person obtaining a copy
16+
* of this software and associated documentation files (the "Software"), to deal
17+
* in the Software without restriction, including without limitation the rights
18+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19+
* copies of the Software, and to permit persons to whom the Software is
20+
* furnished to do so, subject to the following conditions:
21+
* <p>
22+
* The above copyright notice and this permission notice shall be included in all
23+
* copies or substantial portions of the Software.
24+
* <p>
25+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31+
* SOFTWARE.
32+
*/
33+
public class FragmentActivityJava extends AppCompatActivity {
34+
35+
RoomBackup roomBackup;
36+
37+
@Override
38+
protected void onCreate(Bundle savedInstanceState) {
39+
super.onCreate(savedInstanceState);
40+
setContentView(R.layout.activity_fragment);
41+
42+
roomBackup = new RoomBackup(this);
43+
44+
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
45+
transaction.replace(R.id.fragment_container_view, new MainFragmentJava());
46+
transaction.commit();
47+
}
48+
}

app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/MainActivity.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class MainActivity : AppCompatActivity(), FruitListAdapter.OnItemClickListener {
7676
val btnRestore: Button = findViewById(R.id.btn_restore)
7777
val btnProperties: Button = findViewById(R.id.btn_properties)
7878
val btnLanguage: Button = findViewById(R.id.btn_switch_language)
79+
val btnFragmentActivity: Button = findViewById(R.id.btn_switch_fragment_activity)
7980
val btnBackupLocation: Button = findViewById(R.id.btn_backup_location)
8081
val tvFruits: TextView = findViewById(R.id.tv_fruits)
8182

@@ -85,12 +86,13 @@ class MainActivity : AppCompatActivity(), FruitListAdapter.OnItemClickListener {
8586

8687
fruitViewModel = ViewModelProvider(this)[FruitViewModel::class.java]
8788

88-
fruitViewModel.allFruit.observe(this, { fruits ->
89+
fruitViewModel.allFruit.observe(this) { fruits ->
8990
adapter.submitList(fruits)
90-
})
91+
}
9192

92-
tvFruits.text = "Fruits List (Kotlin)"
93+
tvFruits.text = "Fruits List (Kotlin Activity)"
9394
btnLanguage.text = "switch to Java"
95+
btnFragmentActivity.text = "switch to Fragment"
9496

9597
val sharedPrefs = "sampleBackup"
9698
val spEncryptBackup = "encryptBackup"
@@ -112,6 +114,13 @@ class MainActivity : AppCompatActivity(), FruitListAdapter.OnItemClickListener {
112114
startActivity(intent)
113115
}
114116

117+
/*---------------------go to Fragment class--------------------------*/
118+
btnFragmentActivity.setOnClickListener {
119+
finish()
120+
val intent = Intent(this, FragmentActivity::class.java)
121+
startActivity(intent)
122+
}
123+
115124
var encryptBackup = sharedPreferences.getBoolean(spEncryptBackup, true)
116125
var storageLocation = sharedPreferences.getInt(spStorageLocation, 1)
117126
var enableLog = sharedPreferences.getBoolean(spEnableLog, true)

app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/MainActivityJava.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ protected void onCreate(Bundle savedInstanceState) {
100100
Button btn_backup = findViewById(R.id.btn_backup);
101101
Button btn_restore = findViewById(R.id.btn_restore);
102102
Button btn_language = findViewById(R.id.btn_switch_language);
103+
Button btn_fragment_activity = findViewById(R.id.btn_switch_fragment_activity);
103104
Button btn_properties = findViewById(R.id.btn_properties);
104105
Button btn_backupLocation = findViewById(R.id.btn_backup_location);
105106
TextView tvFruits = findViewById(R.id.tv_fruits);
@@ -114,8 +115,9 @@ protected void onCreate(Bundle savedInstanceState) {
114115

115116
fruitViewModel.getAllFruit().observe(this, adapter::submitList);
116117

117-
tvFruits.setText("Fruits List (Java)");
118+
tvFruits.setText("Fruits List (Java Activity)");
118119
btn_language.setText("switch to Kotlin");
120+
btn_fragment_activity.setText("switch to Fragment");
119121

120122
String SHARED_PREFS = "sampleBackup";
121123
final String spEncryptBackup = "encryptBackup";
@@ -137,6 +139,13 @@ protected void onCreate(Bundle savedInstanceState) {
137139
startActivity(intent);
138140
});
139141

142+
/*---------------------go to Fragment class--------------------------*/
143+
btn_fragment_activity.setOnClickListener(v -> {
144+
finish();
145+
Intent intent = new Intent(getApplicationContext(), FragmentActivityJava.class);
146+
startActivity(intent);
147+
});
148+
140149

141150
encryptBackup = sharedPreferences.getBoolean(spEncryptBackup, true);
142151
storageLocation = sharedPreferences.getInt(spStorageLocation, 1);

0 commit comments

Comments
 (0)